
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.analysis.defects.dilute_solution_model &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script src="../../../../_static/jquery.js"></script>
    <script src="../../../../_static/underscore.js"></script>
    <script src="../../../../_static/doctools.js"></script>
    <script src="../../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.defects.dilute_solution_model</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.analysis.defects.dilute_solution_model</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Evaluate the defect concentration based on composition, temperature,</span>
<span class="sd">and defect energies using &quot;Dilute Solution Model&quot;</span>
<span class="sd">Reference: Phys Rev B, 63, 094103, 2001,</span>
<span class="sd">&quot;Density of constitutional and thermal point defects in L12 Al3Sc&quot;,</span>
<span class="sd">C. Woodward, M. Asta, G. Kresse and J. Hafner.</span>
<span class="sd">Manual and citation for the code, DOI: 10.1016/j.cpc.2015.03.015</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">monty.fractions</span> <span class="kn">import</span> <span class="n">gcd</span>
<span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span><span class="p">,</span> <span class="n">nsolve</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">Float</span><span class="p">,</span> <span class="n">Matrix</span><span class="p">,</span> <span class="n">exp</span><span class="p">,</span> <span class="n">solve</span>

<span class="kn">from</span> <span class="nn">monty.dev</span> <span class="kn">import</span> <span class="n">deprecated</span>


<span class="n">__author__</span> <span class="o">=</span> <span class="s1">&#39;Bharat Medasani&#39;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;0.2&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Bharat Medasani&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;mbkumar@gmail.com&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s2">&quot;Alpha&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;6/4/14&quot;</span>

<span class="c1"># physical consts</span>
<span class="n">k_B</span> <span class="o">=</span> <span class="mf">8.6173324e-5</span>  <span class="c1"># eV/K</span>


<span class="c1"># Check the inputs</span>
<span class="k">def</span> <span class="nf">_check_input</span><span class="p">(</span><span class="n">def_list</span><span class="p">):</span>
    <span class="n">flag</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="k">for</span> <span class="n">defect</span> <span class="ow">in</span> <span class="n">def_list</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">defect</span><span class="p">:</span>
            <span class="n">flag</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">break</span>
    <span class="k">return</span> <span class="n">flag</span>


<span class="nd">@deprecated</span><span class="p">(</span><span class="n">message</span><span class="o">=</span><span class="s2">&quot;Refactoring of the defects module will eventualy remove this function&quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">dilute_solution_model</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">e0</span><span class="p">,</span> <span class="n">vac_defs</span><span class="p">,</span> <span class="n">antisite_defs</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">trial_chem_pot</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">generate</span><span class="o">=</span><span class="s1">&#39;plot&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the defect densities using dilute solution model.</span>

<span class="sd">    Args:</span>
<span class="sd">        structure: pymatgen.core.structure.Structure object representing the</span>
<span class="sd">            primitive or unitcell of the crystal.</span>
<span class="sd">        e0: The total energy of the undefected system.</span>
<span class="sd">            This is E0 from VASP calculation.</span>
<span class="sd">        vac_defs: List of vacancy defect parameters in the dictionary format.</span>
<span class="sd">            The keys of the dict associated with each vacancy defect are</span>
<span class="sd">            1) site_index, 2) site_specie, 3) site_multiplicity, and</span>
<span class="sd">            4) energy. 1-3 can be obtained from</span>
<span class="sd">            pymatgen.analysis.defects.point_defects.Vacancy class.</span>
<span class="sd">            Site index is expected to start with 1 (fortran index).</span>
<span class="sd">        antisite_defs: List of antisite defect parameters in the dictionary</span>
<span class="sd">            format. The keys of the dict associated with each antisite defect</span>
<span class="sd">            are 1) site_index, 2) site_specie, 3) site_multiplicity,</span>
<span class="sd">            4) substitution_specie, and 5) energy. 1-3 can be obtained</span>
<span class="sd">            from pymatgen.analysis.defects.point_defects.Vacancy class.</span>
<span class="sd">        T: Temperature in Kelvin</span>
<span class="sd">        trial_chem_pot (optional): Trial chemical potentials to speedup</span>
<span class="sd">            the plot generation. Format is {el1:mu1,...}</span>
<span class="sd">        generate (string): Options are plot or energy</span>
<span class="sd">            Chemical potentials are also returned with energy option.</span>
<span class="sd">            If energy option is not chosen, plot is generated.</span>

<span class="sd">    Returns:</span>
<span class="sd">        If generate=plot, the plot data is generated and returned in</span>
<span class="sd">        HighCharts format.</span>
<span class="sd">        If generate=energy, defect formation enthalpies and chemical</span>
<span class="sd">        potentials are returned.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">_check_input</span><span class="p">(</span><span class="n">vac_defs</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Vacancy energy is not defined&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">_check_input</span><span class="p">(</span><span class="n">antisite_defs</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Antisite energy is not defined&#39;</span><span class="p">)</span>

    <span class="n">formation_energies</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;vacancies&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">vac_defs</span><span class="p">)</span>
    <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;antisites&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">antisite_defs</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">vac</span> <span class="ow">in</span> <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;vacancies&#39;</span><span class="p">]:</span>
        <span class="k">del</span> <span class="n">vac</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
    <span class="k">for</span> <span class="n">asite</span> <span class="ow">in</span> <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;antisites&#39;</span><span class="p">]:</span>
        <span class="k">del</span> <span class="n">asite</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
    <span class="c1"># Setup the system</span>
    <span class="n">site_species</span> <span class="o">=</span> <span class="p">[</span><span class="n">vac_def</span><span class="p">[</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">vac_def</span> <span class="ow">in</span> <span class="n">vac_defs</span><span class="p">]</span>
    <span class="n">multiplicity</span> <span class="o">=</span> <span class="p">[</span><span class="n">vac_def</span><span class="p">[</span><span class="s1">&#39;site_multiplicity&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">vac_def</span> <span class="ow">in</span> <span class="n">vac_defs</span><span class="p">]</span>
    <span class="n">m</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">site_species</span><span class="p">))</span>  <span class="c1"># distinct species</span>
    <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">vac_defs</span><span class="p">)</span>  <span class="c1"># inequivalent sites</span>

    <span class="c1"># Reduce the system and associated parameters such that only distinctive</span>
    <span class="c1"># atoms are retained</span>
    <span class="n">comm_div</span> <span class="o">=</span> <span class="n">gcd</span><span class="p">(</span><span class="o">*</span><span class="nb">tuple</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">))</span>
    <span class="n">multiplicity</span> <span class="o">=</span> <span class="p">[</span><span class="n">val</span> <span class="o">/</span> <span class="n">comm_div</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">multiplicity</span><span class="p">]</span>
    <span class="n">e0</span> <span class="o">=</span> <span class="n">e0</span> <span class="o">/</span> <span class="n">comm_div</span>
    <span class="n">T</span> <span class="o">=</span> <span class="n">Float</span><span class="p">(</span><span class="n">T</span><span class="p">)</span>

    <span class="c1"># c0 = np.diag(multiplicity)</span>
    <span class="n">c0</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">diag</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">n</span><span class="p">))</span>
    <span class="n">mu</span> <span class="o">=</span> <span class="p">[</span><span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;mu&#39;</span> <span class="o">+</span> <span class="n">i</span><span class="o">.</span><span class="fm">__str__</span><span class="p">())</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]</span>

    <span class="c1"># Generate maps for hashing</span>
    <span class="c1"># Generate specie-&gt;mu map and use it for site-&gt;mu map</span>
    <span class="n">specie_order</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># Contains hash for site-&gt;mu map    Eg: [Al, Ni]</span>
    <span class="n">site_specie_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>  <span class="c1"># Eg: {Ni, Al}</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="n">site_specie</span> <span class="o">=</span> <span class="n">site_species</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">site_specie</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">site_specie_set</span><span class="p">:</span>
            <span class="n">site_specie_set</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">site_specie</span><span class="p">)</span>
            <span class="n">specie_order</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">site_specie</span><span class="p">)</span>
    <span class="n">site_mu_map</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># Eg: [mu0,mu0,mu0,mu1] where mu0-&gt;Al, and mu1-&gt;Ni</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="n">site_specie</span> <span class="o">=</span> <span class="n">site_species</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">j</span> <span class="o">=</span> <span class="n">specie_order</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">site_specie</span><span class="p">)</span>
        <span class="n">site_mu_map</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">j</span><span class="p">)</span>
    <span class="n">specie_site_index_map</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># Eg: [(0,3),(3,4)] for Al &amp; Ni</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">):</span>
        <span class="n">low_ind</span> <span class="o">=</span> <span class="n">site_species</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">specie_order</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">m</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">hgh_ind</span> <span class="o">=</span> <span class="n">site_species</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">specie_order</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">hgh_ind</span> <span class="o">=</span> <span class="n">n</span>
        <span class="n">specie_site_index_map</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">low_ind</span><span class="p">,</span> <span class="n">hgh_ind</span><span class="p">))</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    dC: delta concentration matrix:</span>
<span class="sd">    dC[i,j,k]: Concentration change of atom i, due to presence of atom</span>
<span class="sd">    j on lattice site k</span>
<span class="sd">    Special case is [i,i,i] which is considered as vacancy</span>
<span class="sd">    Few cases: dC[i,i,i] = -1 due to being vacancy special case</span>
<span class="sd">                dC[k,k,i] = +1 due to increment in k at i lattice if i</span>
<span class="sd">                               lattice type is of different element</span>
<span class="sd">                dC[i,k,i] = -1 due to decrement of ith type atom due to</span>
<span class="sd">                presence of kth type atom on ith sublattice and kth type</span>
<span class="sd">                atom specie is different from ith sublattice atom specie</span>
<span class="sd">                dC[i,k,k] = 0 due to no effect on ith type atom</span>
<span class="sd">                dC[i,j,k] = 0 if i!=j!=k</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">dC</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">j</span> <span class="ow">and</span> <span class="n">site_species</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">!=</span> <span class="n">site_species</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="ow">and</span> \
                        <span class="n">site_species</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">!=</span> <span class="n">site_species</span><span class="p">[</span><span class="n">k</span><span class="p">]:</span>
                    <span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">k</span><span class="p">:</span>
                    <span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
    <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">!=</span> <span class="n">j</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">site_species</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">==</span> <span class="n">site_species</span><span class="p">[</span><span class="n">k</span><span class="p">]:</span>
                        <span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">for</span> <span class="n">ind_map</span> <span class="ow">in</span> <span class="n">specie_site_index_map</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">ind_map</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">ind_map</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">index1</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ind_map</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">ind_map</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
                <span class="k">for</span> <span class="n">index2</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ind_map</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                        <span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">index1</span><span class="p">,</span> <span class="n">index2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">for</span> <span class="n">index2</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ind_map</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">n</span><span class="p">):</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                        <span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">index1</span><span class="p">,</span> <span class="n">index2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="c1"># dE matrix: Flip energies (or raw defect energies)</span>
    <span class="n">els</span> <span class="o">=</span> <span class="p">[</span><span class="n">vac_def</span><span class="p">[</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">vac_def</span> <span class="ow">in</span> <span class="n">vac_defs</span><span class="p">]</span>
    <span class="n">dE</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="n">dE</span><span class="o">.</span><span class="n">append</span><span class="p">([])</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">dE</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">j</span><span class="p">:</span>
                <span class="n">dE</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">vac_defs</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">sub_specie</span> <span class="o">=</span> <span class="n">vac_defs</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span>
                <span class="n">site_specie</span> <span class="o">=</span> <span class="n">vac_defs</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">site_specie</span> <span class="o">==</span> <span class="n">sub_specie</span><span class="p">:</span>
                    <span class="n">dE</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">as_def</span> <span class="ow">in</span> <span class="n">antisite_defs</span><span class="p">:</span>
                        <span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;site_index&#39;</span><span class="p">])</span> <span class="o">==</span> <span class="n">j</span> <span class="o">+</span> <span class="mi">1</span> <span class="ow">and</span> \
                                <span class="n">sub_specie</span> <span class="o">==</span> <span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;substitution_specie&#39;</span><span class="p">]:</span>
                            <span class="n">dE</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
                            <span class="k">break</span>
    <span class="n">dE</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">dE</span><span class="p">)</span>

    <span class="c1"># Initialization for concentrations</span>
    <span class="c1"># c(i,p) == presence of ith type atom on pth type site</span>
    <span class="n">c</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">p</span><span class="p">]</span> <span class="o">=</span> <span class="n">Integer</span><span class="p">(</span><span class="n">c0</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">p</span><span class="p">])</span>
            <span class="n">site_flip_contribs</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">epi</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                <span class="n">sum_mu</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">mu</span><span class="p">[</span><span class="n">site_mu_map</span><span class="p">[</span><span class="n">j</span><span class="p">]]</span> <span class="o">*</span> <span class="n">Integer</span><span class="p">(</span><span class="n">dC</span><span class="p">[</span><span class="n">j</span><span class="p">,</span> <span class="n">epi</span><span class="p">,</span> <span class="n">p</span><span class="p">])</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)])</span>
                <span class="n">flip</span> <span class="o">=</span> <span class="n">Integer</span><span class="p">(</span><span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">epi</span><span class="p">,</span> <span class="n">p</span><span class="p">])</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">dE</span><span class="p">[</span><span class="n">epi</span><span class="p">,</span> <span class="n">p</span><span class="p">]</span> <span class="o">-</span> <span class="n">sum_mu</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">flip</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">site_flip_contribs</span><span class="p">:</span>
                    <span class="n">site_flip_contribs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">flip</span><span class="p">)</span>
                    <span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">p</span><span class="p">]</span> <span class="o">+=</span> <span class="n">flip</span>

    <span class="n">total_c</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="n">specie_site_index_map</span><span class="p">:</span>
        <span class="n">val</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="o">*</span><span class="n">ind</span><span class="p">):</span>
            <span class="n">sum_i</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">*</span> <span class="n">multiplicity</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)])</span>
            <span class="n">val</span> <span class="o">+=</span> <span class="n">sum_i</span>
        <span class="n">total_c</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>

    <span class="n">c_ratio</span> <span class="o">=</span> <span class="p">[</span><span class="n">total_c</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">/</span> <span class="n">total_c</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]</span>

    <span class="c1"># Expression for Omega, the Grand Potential</span>
    <span class="n">omega1</span> <span class="o">=</span> <span class="n">e0</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">([</span><span class="n">mu</span><span class="p">[</span><span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">*</span> <span class="nb">sum</span><span class="p">(</span><span class="n">c0</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:])</span> <span class="o">*</span> <span class="n">multiplicity</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)])</span>
    <span class="n">omega2</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">fm_en_eff</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">used_dEs</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">p_r</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">epi</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">sum_mu</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">mu</span><span class="p">[</span><span class="n">site_mu_map</span><span class="p">[</span><span class="n">j</span><span class="p">]]</span> <span class="o">*</span> <span class="n">dC</span><span class="p">[</span><span class="n">j</span><span class="p">,</span> <span class="n">epi</span><span class="p">,</span> <span class="n">p_r</span><span class="p">]</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)])</span>
            <span class="k">if</span> <span class="n">p_r</span> <span class="o">!=</span> <span class="n">epi</span> <span class="ow">and</span> <span class="n">site_mu_map</span><span class="p">[</span><span class="n">p_r</span><span class="p">]</span> <span class="o">==</span> <span class="n">site_mu_map</span><span class="p">[</span><span class="n">epi</span><span class="p">]:</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="n">dE</span><span class="p">[</span><span class="n">epi</span><span class="p">,</span> <span class="n">p_r</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">used_dEs</span><span class="p">:</span>
                <span class="n">omega2</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span> <span class="o">*</span> <span class="n">multiplicity</span><span class="p">[</span><span class="n">p_r</span><span class="p">]</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">dE</span><span class="p">[</span><span class="n">epi</span><span class="p">,</span> <span class="n">p_r</span><span class="p">]</span> <span class="o">-</span> <span class="n">sum_mu</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span><span class="p">)))</span>
                <span class="n">fm_en_eff</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dE</span><span class="p">[</span><span class="n">epi</span><span class="p">,</span> <span class="n">p_r</span><span class="p">]</span> <span class="o">-</span> <span class="n">sum_mu</span><span class="p">)</span>
                <span class="n">used_dEs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dE</span><span class="p">[</span><span class="n">epi</span><span class="p">,</span> <span class="n">p_r</span><span class="p">])</span>
    <span class="n">omega</span> <span class="o">=</span> <span class="n">omega1</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">(</span><span class="n">omega2</span><span class="p">)</span>

    <span class="c1"># Compute composition range</span>
    <span class="n">li</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">hi</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">comp1_min</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">[</span><span class="n">li</span><span class="p">:</span><span class="n">hi</span><span class="p">])</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span> <span class="o">-</span> <span class="mi">1</span>
    <span class="n">comp1_max</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">[</span><span class="n">li</span><span class="p">:</span><span class="n">hi</span><span class="p">])</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="n">delta</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">comp1_max</span> <span class="o">-</span> <span class="n">comp1_min</span><span class="p">)</span> <span class="o">/</span> <span class="mf">120.0</span>
    <span class="n">yvals</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">comp1</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">comp1_min</span><span class="p">,</span> <span class="n">comp1_max</span> <span class="o">+</span> <span class="n">delta</span><span class="p">,</span> <span class="n">delta</span><span class="p">):</span>
        <span class="n">comp2</span> <span class="o">=</span> <span class="mi">100</span> <span class="o">-</span> <span class="n">comp1</span>
        <span class="n">y</span> <span class="o">=</span> <span class="n">comp2</span> <span class="o">/</span> <span class="n">comp1</span>
        <span class="n">yvals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">reduce_mu</span><span class="p">():</span>
        <span class="n">omega</span> <span class="o">=</span> <span class="p">[</span><span class="n">e0</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">([</span><span class="n">mu</span><span class="p">[</span><span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">*</span> <span class="nb">sum</span><span class="p">(</span><span class="n">c0</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:])</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)])]</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">solve</span><span class="p">(</span><span class="n">omega</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">x</span>

    <span class="k">def</span> <span class="nf">compute_mus_by_search</span><span class="p">():</span>
        <span class="c1"># Compute trial mu</span>
        <span class="n">mu_red</span> <span class="o">=</span> <span class="n">reduce_mu</span><span class="p">()</span>

        <span class="n">mult</span> <span class="o">=</span> <span class="n">multiplicity</span>
        <span class="n">specie_concen</span> <span class="o">=</span> <span class="p">[</span><span class="nb">sum</span><span class="p">(</span><span class="n">mult</span><span class="p">[</span><span class="n">ind</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">ind</span><span class="p">[</span><span class="mi">1</span><span class="p">]])</span> <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="n">specie_site_index_map</span><span class="p">]</span>
        <span class="n">y_vect</span> <span class="o">=</span> <span class="p">[</span><span class="n">specie_concen</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">/</span> <span class="n">specie_concen</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]</span>
        <span class="n">vector_func</span> <span class="o">=</span> <span class="p">[</span><span class="n">y_vect</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">c_ratio</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span>
        <span class="n">vector_func</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">omega</span><span class="p">)</span>
        <span class="n">min_diff</span> <span class="o">=</span> <span class="mf">1e10</span>
        <span class="n">mu_vals</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">c_val</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">m1_min</span> <span class="o">=</span> <span class="o">-</span><span class="mf">20.0</span>
        <span class="k">if</span> <span class="n">e0</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">m1_max</span> <span class="o">=</span> <span class="mi">10</span>  <span class="c1"># Search space needs to be modified</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">m1_max</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">m1</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">m1_min</span><span class="p">,</span> <span class="n">m1_max</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">):</span>
            <span class="n">m0</span> <span class="o">=</span> <span class="n">mu_red</span><span class="p">[</span><span class="n">mu</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">mu</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">m1</span><span class="p">)</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="n">x</span> <span class="o">=</span> <span class="n">nsolve</span><span class="p">(</span><span class="n">vector_func</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="p">[</span><span class="n">m0</span><span class="p">,</span> <span class="n">m1</span><span class="p">],</span> <span class="n">module</span><span class="o">=</span><span class="s2">&quot;numpy&quot;</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="n">c_val</span> <span class="o">=</span> <span class="n">c</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">x</span><span class="p">)))</span>
            <span class="c1"># if all(x &gt;= 0 for x in c_val):</span>
            <span class="n">specie_concen</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="n">specie_site_index_map</span><span class="p">:</span>
                <span class="n">specie_concen</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">sum</span><span class="p">([</span><span class="nb">sum</span><span class="p">(</span><span class="n">c_val</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:])</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="o">*</span><span class="n">ind</span><span class="p">)]))</span>
            <span class="n">y_comp</span> <span class="o">=</span> <span class="p">[</span><span class="n">specie_concen</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">/</span> <span class="n">specie_concen</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]</span>
            <span class="n">diff</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="nb">sum</span><span class="p">([</span><span class="nb">pow</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">y_comp</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">y_vect</span><span class="p">[</span><span class="n">i</span><span class="p">]),</span> <span class="mi">2</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]))</span>
            <span class="k">if</span> <span class="n">diff</span> <span class="o">&lt;</span> <span class="n">min_diff</span><span class="p">:</span>
                <span class="n">min_diff</span> <span class="o">=</span> <span class="n">diff</span>
                <span class="n">mu_vals</span> <span class="o">=</span> <span class="n">x</span>

        <span class="k">if</span> <span class="n">mu_vals</span><span class="p">:</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">mu_val</span><span class="p">)</span> <span class="k">for</span> <span class="n">mu_val</span> <span class="ow">in</span> <span class="n">mu_vals</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">mu_vals</span>

    <span class="k">def</span> <span class="nf">compute_def_formation_energies</span><span class="p">():</span>
        <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">vac_def</span> <span class="ow">in</span> <span class="n">vac_defs</span><span class="p">:</span>
            <span class="n">site_specie</span> <span class="o">=</span> <span class="n">vac_def</span><span class="p">[</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span>
            <span class="n">ind</span> <span class="o">=</span> <span class="n">specie_order</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">site_specie</span><span class="p">)</span>
            <span class="n">uncor_energy</span> <span class="o">=</span> <span class="n">vac_def</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
            <span class="n">formation_energy</span> <span class="o">=</span> <span class="n">uncor_energy</span> <span class="o">+</span> <span class="n">mu_vals</span><span class="p">[</span><span class="n">ind</span><span class="p">]</span>
            <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;vacancies&#39;</span><span class="p">][</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;formation_energy&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">formation_energy</span>
            <span class="n">specie_ind</span> <span class="o">=</span> <span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="n">indices</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="n">specie_ind</span><span class="p">]</span>
            <span class="n">specie_ind_del</span> <span class="o">=</span> <span class="n">indices</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">cur_ind</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">specie_ind_del</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;$V_{&#39;</span> <span class="o">+</span> <span class="n">site_specie</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;$V_{&#39;</span> <span class="o">+</span> <span class="n">site_specie</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">cur_ind</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
            <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;vacancies&#39;</span><span class="p">][</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;label&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">label</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">as_def</span> <span class="ow">in</span> <span class="n">antisite_defs</span><span class="p">:</span>
            <span class="n">site_specie</span> <span class="o">=</span> <span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span>
            <span class="n">sub_specie</span> <span class="o">=</span> <span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;substitution_specie&#39;</span><span class="p">]</span>
            <span class="n">ind1</span> <span class="o">=</span> <span class="n">specie_order</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">site_specie</span><span class="p">)</span>
            <span class="n">ind2</span> <span class="o">=</span> <span class="n">specie_order</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">sub_specie</span><span class="p">)</span>
            <span class="n">uncor_energy</span> <span class="o">=</span> <span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
            <span class="n">formation_energy</span> <span class="o">=</span> <span class="n">uncor_energy</span> <span class="o">+</span> <span class="n">mu_vals</span><span class="p">[</span><span class="n">ind1</span><span class="p">]</span> <span class="o">-</span> <span class="n">mu_vals</span><span class="p">[</span><span class="n">ind2</span><span class="p">]</span>
            <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;antisites&#39;</span><span class="p">][</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;formation_energy&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">formation_energy</span>
            <span class="n">specie_ind</span> <span class="o">=</span> <span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="n">indices</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="n">specie_ind</span><span class="p">]</span>
            <span class="n">specie_ind_del</span> <span class="o">=</span> <span class="n">indices</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">cur_ind</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">specie_ind_del</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;$&#39;</span> <span class="o">+</span> <span class="n">sub_specie</span> <span class="o">+</span> <span class="s1">&#39;_{&#39;</span> <span class="o">+</span> <span class="n">site_specie</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;$&#39;</span> <span class="o">+</span> <span class="n">sub_specie</span> <span class="o">+</span> <span class="s1">&#39;_{&#39;</span> <span class="o">+</span> <span class="n">site_specie</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">cur_ind</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
            <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;antisites&#39;</span><span class="p">][</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;label&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">label</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="n">formation_energies</span>

    <span class="c1"># If generate option is energy compute effective formation energies</span>
    <span class="c1"># at ideal stoichiometry and return the formation energies and chem pot.</span>
    <span class="k">if</span> <span class="n">generate</span> <span class="o">==</span> <span class="s1">&#39;energy&#39;</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">trial_chem_pot</span><span class="p">:</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="n">compute_mus_by_search</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="n">trial_chem_pot</span><span class="p">[</span><span class="n">element</span><span class="p">]</span> <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">specie_order</span><span class="p">]</span>

        <span class="n">formation_energies</span> <span class="o">=</span> <span class="n">compute_def_formation_energies</span><span class="p">()</span>
        <span class="n">mu_dict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">specie_order</span><span class="p">,</span> <span class="n">mu_vals</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">formation_energies</span><span class="p">,</span> <span class="n">mu_dict</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">trial_chem_pot</span><span class="p">:</span>
        <span class="c1"># Try computing mus by assuming one of the defects is dominant at 0.01</span>
        <span class="c1"># concen.  First vacancy is tried and then antisite</span>

        <span class="c1"># Generate trial mus assuming vacancy as dominant defect</span>
        <span class="c1"># for specie-0 at lower yval</span>
        <span class="n">li</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">hi</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">li1</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">hi1</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">spec_mult</span> <span class="o">=</span> <span class="p">[</span><span class="nb">sum</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">[</span><span class="n">li</span><span class="p">:</span><span class="n">hi</span><span class="p">]),</span> <span class="nb">sum</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">[</span><span class="n">li1</span><span class="p">:</span><span class="n">hi1</span><span class="p">])]</span>
        <span class="n">ln_def_conc</span> <span class="o">=</span> <span class="mf">4.60517</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">li</span><span class="p">,</span> <span class="n">hi</span><span class="p">):</span>
            <span class="n">vac_flip_en</span> <span class="o">=</span> <span class="n">vac_defs</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="n">ln_def_conc</span> <span class="o">*</span> <span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span> <span class="o">-</span> <span class="n">vac_flip_en</span><span class="p">]</span>
            <span class="n">mu_vals</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">e0</span> <span class="o">-</span> <span class="n">spec_mult</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">mu_vals</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">/</span> <span class="n">spec_mult</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="n">comp_ratio</span> <span class="o">=</span> <span class="n">yvals</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

            <span class="c1"># Test if the trial mus are good</span>
            <span class="n">vector_func</span> <span class="o">=</span> <span class="p">[</span><span class="n">comp_ratio</span> <span class="o">-</span> <span class="n">c_ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
            <span class="n">vector_func</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">omega</span><span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">mu_vals</span> <span class="o">=</span> <span class="n">nsolve</span><span class="p">(</span><span class="n">vector_func</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">mu_vals</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">mu_vals</span><span class="p">:</span>
                    <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">mu_val</span><span class="p">)</span> <span class="k">for</span> <span class="n">mu_val</span> <span class="ow">in</span> <span class="n">mu_vals</span><span class="p">]</span>
                <span class="k">break</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>  <span class="c1"># Go for antisite as dominant defect</span>
                <span class="n">mu_gs</span> <span class="o">=</span> <span class="p">[</span><span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;mu_gs&#39;</span> <span class="o">+</span> <span class="n">j</span><span class="o">.</span><span class="fm">__str__</span><span class="p">())</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]</span>

                <span class="n">eqs</span> <span class="o">=</span> <span class="p">[</span><span class="n">mu_gs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">mu_gs</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="p">(</span><span class="n">ln_def_conc</span> <span class="o">*</span> <span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span> <span class="o">-</span> <span class="n">antisite_defs</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;energy&#39;</span><span class="p">])]</span>
                <span class="n">eqs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">spec_mult</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">mu_gs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">spec_mult</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">mu_gs</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">e0</span><span class="p">)</span>
                <span class="n">x</span> <span class="o">=</span> <span class="n">solve</span><span class="p">(</span><span class="n">eqs</span><span class="p">,</span> <span class="n">mu_gs</span><span class="p">)</span>
                <span class="c1"># mu_names = sorted([key.name for key in x.keys()])</span>
                <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">keys</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">inp</span><span class="p">:</span> <span class="n">inp</span><span class="o">.</span><span class="n">name</span><span class="p">):</span>
                    <span class="n">mu_vals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">key</span><span class="p">])</span>
                <span class="n">vector_func</span> <span class="o">=</span> <span class="p">[</span><span class="n">comp_ratio</span> <span class="o">-</span> <span class="n">c_ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
                <span class="n">vector_func</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">omega</span><span class="p">)</span>

                <span class="k">try</span><span class="p">:</span>
                    <span class="n">mu_vals</span> <span class="o">=</span> <span class="n">nsolve</span><span class="p">(</span><span class="n">vector_func</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">mu_vals</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">mu_vals</span><span class="p">:</span>
                        <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">mu_val</span><span class="p">)</span> <span class="k">for</span> <span class="n">mu_val</span> <span class="ow">in</span> <span class="n">mu_vals</span><span class="p">]</span>
                    <span class="k">break</span>
                <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>  <span class="c1"># Go to the default option (search the space)</span>
                    <span class="k">pass</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="n">compute_mus_by_search</span><span class="p">()</span>

    <span class="k">else</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="n">trial_chem_pot</span><span class="p">[</span><span class="n">element</span><span class="p">]</span> <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">specie_order</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="n">compute_mus_by_search</span><span class="p">()</span>

    <span class="c1"># Compile mu&#39;s for all composition ratios in the range</span>
    <span class="c1"># +/- 1% from the stoichiometry</span>
    <span class="n">result</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">failed_y</span><span class="p">,</span> <span class="n">failed_i</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">yvals</span><span class="p">:</span>
        <span class="n">vector_func</span> <span class="o">=</span> <span class="p">[</span><span class="n">y</span> <span class="o">-</span> <span class="n">c_ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
        <span class="n">vector_func</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">omega</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">x</span> <span class="o">=</span> <span class="n">nsolve</span><span class="p">(</span><span class="n">vector_func</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">mu_vals</span><span class="p">,</span> <span class="n">module</span><span class="o">=</span><span class="s2">&quot;numpy&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">x</span><span class="p">:</span>
                <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">mu_val</span><span class="p">)</span> <span class="k">for</span> <span class="n">mu_val</span> <span class="ow">in</span> <span class="n">x</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="n">failed_y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
            <span class="n">failed_i</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="k">continue</span>
        <span class="n">result</span><span class="p">[</span><span class="n">y</span><span class="p">]</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">mu_vals</span><span class="p">)</span>
        <span class="n">x</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">get_next_mu_val</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="n">yvals</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">None</span>

        <span class="n">y</span> <span class="o">=</span> <span class="n">yvals</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">x</span><span class="p">:</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">mu_val</span><span class="p">)</span> <span class="k">for</span> <span class="n">mu_val</span> <span class="ow">in</span> <span class="n">x</span><span class="p">]</span>
            <span class="k">return</span> <span class="n">mu_vals</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">get_next_mu_val</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_prev_mu_val</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>

        <span class="n">y</span> <span class="o">=</span> <span class="n">yvals</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">x</span><span class="p">:</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">mu_val</span><span class="p">)</span> <span class="k">for</span> <span class="n">mu_val</span> <span class="ow">in</span> <span class="n">x</span><span class="p">]</span>
            <span class="k">return</span> <span class="n">mu_vals</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">get_next_mu_val</span><span class="p">(</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>

    <span class="c1"># Try to get better trial mus for failed cases</span>
    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">failed_y</span><span class="p">)):</span>
        <span class="n">i</span> <span class="o">=</span> <span class="n">failed_i</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>

        <span class="n">prev_mu_val</span> <span class="o">=</span> <span class="n">get_prev_mu_val</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">prev_mu_val</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="n">next_mu_val</span> <span class="o">=</span> <span class="n">get_next_mu_val</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">next_mu_val</span><span class="p">:</span>
            <span class="k">continue</span>

        <span class="n">y</span> <span class="o">=</span> <span class="n">failed_y</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
        <span class="n">vector_func</span> <span class="o">=</span> <span class="p">[</span><span class="n">y</span> <span class="o">-</span> <span class="n">c_ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
        <span class="n">vector_func</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">omega</span><span class="p">)</span>
        <span class="n">trial_mu</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">float</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">x</span><span class="p">))</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="nb">zip</span><span class="p">(</span><span class="n">prev_mu_val</span><span class="p">,</span> <span class="n">next_mu_val</span><span class="p">)))</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">x</span> <span class="o">=</span> <span class="n">nsolve</span><span class="p">(</span><span class="n">vector_func</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">trial_mu</span><span class="p">,</span> <span class="n">module</span><span class="o">=</span><span class="s2">&quot;numpy&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">x</span><span class="p">:</span>
                <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">mu_val</span><span class="p">)</span> <span class="k">for</span> <span class="n">mu_val</span> <span class="ow">in</span> <span class="n">x</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="n">result</span><span class="p">[</span><span class="n">y</span><span class="p">]</span> <span class="o">=</span> <span class="n">mu_vals</span>
        <span class="n">x</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="c1"># Alternate way of calculating trial mus for failed cases</span>
    <span class="c1"># by taking average of trial mus at extremes.</span>
    <span class="c1"># for j in range(len(failed_y)):</span>
    <span class="c1">#    y = yvals[0]</span>
    <span class="c1">#    prev_mu_val = result[y]</span>
    <span class="c1">#    y = yvals[-1]</span>
    <span class="c1">#    next_mu_val = result[y]</span>
    <span class="c1">#</span>
    <span class="c1">#    trial_mu = list(map(lambda x: float(sum(x))/len(x),  \</span>
    <span class="c1">#            zip(prev_mu_val,next_mu_val)))</span>
    <span class="c1">#    y = failed_y[j]</span>
    <span class="c1">#    vector_func = [y-c_ratio[0]]</span>
    <span class="c1">#    vector_func.append(omega)</span>
    <span class="c1">#    try:</span>
    <span class="c1">#        x = nsolve(vector_func,mu,trial_mu,module=&quot;numpy&quot;)</span>
    <span class="c1">#        if x:</span>
    <span class="c1">#            mu_vals = [float(mu_val) for mu_val in x]</span>
    <span class="c1">#    except Exception:</span>
    <span class="c1">#        continue</span>
    <span class="c1">#    result[y] = list(mu_vals)</span>

    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">yvals</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Not sufficient data&#39;</span><span class="p">)</span>

    <span class="n">res</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">new_mu_dict</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="c1"># Compute the concentrations for all the compositions</span>
    <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
        <span class="n">mu_val</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
        <span class="n">total_c_val</span> <span class="o">=</span> <span class="p">[</span><span class="n">total_c</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mu_val</span><span class="p">)))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">total_c</span><span class="p">))]</span>
        <span class="n">c_val</span> <span class="o">=</span> <span class="n">c</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mu_val</span><span class="p">)))</span>
        <span class="n">res1</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># Concentration of first element/over total concen</span>
        <span class="n">res1</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">total_c_val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">total_c_val</span><span class="p">)))</span>
        <span class="n">new_mu_dict</span><span class="p">[</span><span class="n">res1</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">mu_val</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">j</span><span class="p">:</span>  <span class="c1"># Vacancy</span>
                    <span class="n">vac_conc</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">mu_val</span><span class="p">[</span><span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">+</span> <span class="n">dE</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">i</span><span class="p">])</span> <span class="o">/</span> <span class="p">(</span><span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span><span class="p">)))</span>
                    <span class="n">res1</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">vac_conc</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>  <span class="c1"># Antisite</span>
                    <span class="n">res1</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">c_val</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">/</span> <span class="n">c0</span><span class="p">[</span><span class="n">j</span><span class="p">,</span> <span class="n">j</span><span class="p">]))</span>
        <span class="n">res</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">res1</span><span class="p">)</span>

    <span class="n">res</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
    <span class="n">dtype</span> <span class="o">=</span> <span class="p">[(</span><span class="nb">str</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)]</span> <span class="o">+</span> <span class="p">[(</span><span class="nb">str</span><span class="p">(</span><span class="s1">&#39;y</span><span class="si">%d%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">)),</span> <span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)]</span>
    <span class="n">res1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">dtype</span><span class="p">),</span> <span class="n">order</span><span class="o">=</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

    <span class="n">conc_data</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="sd">&quot;&quot;&quot;Because all the plots have identical x-points storing it in a</span>
<span class="sd">    single array&quot;&quot;&quot;</span>
    <span class="n">conc_data</span><span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">dat</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">dat</span> <span class="ow">in</span> <span class="n">res1</span><span class="p">]</span>  <span class="c1"># x-axis data</span>
    <span class="c1"># Element whose composition is varied. For x-label</span>
    <span class="n">conc_data</span><span class="p">[</span><span class="s1">&#39;x_label&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">els</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s2">&quot; mole fraction&quot;</span>
    <span class="n">conc_data</span><span class="p">[</span><span class="s1">&#39;y_label&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;Point defect concentration&quot;</span>
    <span class="n">conc</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="n">conc</span><span class="o">.</span><span class="n">append</span><span class="p">([])</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">conc</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">([])</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">y1</span> <span class="o">=</span> <span class="p">[</span><span class="n">dat</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="n">i</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">dat</span> <span class="ow">in</span> <span class="n">res1</span><span class="p">]</span>
            <span class="n">conc</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">y1</span>

    <span class="n">y_data</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">conc</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">specie</span> <span class="o">=</span> <span class="n">els</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">specie_ind</span> <span class="o">=</span> <span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="n">specie_ind</span><span class="p">]</span>
        <span class="n">specie_ind_del</span> <span class="o">=</span> <span class="n">indices</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">cur_ind</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="n">vac_string</span> <span class="o">=</span> <span class="s2">&quot;$Vac_{&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">specie_ind_del</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">label</span> <span class="o">=</span> <span class="n">vac_string</span> <span class="o">+</span> <span class="n">specie</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">label</span> <span class="o">=</span> <span class="n">vac_string</span> <span class="o">+</span> <span class="n">specie</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">cur_ind</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
        <span class="c1"># Plot data and legend info</span>
        <span class="n">y_data</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;data&#39;</span><span class="p">:</span> <span class="n">data</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">label</span><span class="p">})</span>

    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="n">site_specie</span> <span class="o">=</span> <span class="n">els</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">specie_ind</span> <span class="o">=</span> <span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="n">specie_ind</span><span class="p">]</span>
        <span class="n">specie_ind_del</span> <span class="o">=</span> <span class="n">indices</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">cur_ind</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">):</span>  <span class="c1"># Antisite plot dat</span>
            <span class="n">sub_specie</span> <span class="o">=</span> <span class="n">specie_order</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">sub_specie</span> <span class="o">==</span> <span class="n">site_specie</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">specie_ind_del</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;$&#39;</span> <span class="o">+</span> <span class="n">sub_specie</span> <span class="o">+</span> <span class="s1">&#39;_{&#39;</span> <span class="o">+</span> <span class="n">site_specie</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;$&#39;</span> <span class="o">+</span> <span class="n">sub_specie</span> <span class="o">+</span> <span class="s1">&#39;_{&#39;</span> <span class="o">+</span> <span class="n">site_specie</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">cur_ind</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
            <span class="n">inds</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
            <span class="c1"># TODO: Investigate the value below</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">([</span><span class="n">conc</span><span class="p">[</span><span class="n">ind</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="o">*</span><span class="n">inds</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
            <span class="n">y_data</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;data&#39;</span><span class="p">:</span> <span class="n">data</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">label</span><span class="p">})</span>

    <span class="n">conc_data</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">y_data</span>

    <span class="c1"># Compute the  formation energies</span>
    <span class="k">def</span> <span class="nf">compute_vac_formation_energies</span><span class="p">(</span><span class="n">mu_vals</span><span class="p">):</span>
        <span class="n">en</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">vac_def</span> <span class="ow">in</span> <span class="n">vac_defs</span><span class="p">:</span>
            <span class="n">site_specie</span> <span class="o">=</span> <span class="n">vac_def</span><span class="p">[</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span>
            <span class="n">ind</span> <span class="o">=</span> <span class="n">specie_order</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">site_specie</span><span class="p">)</span>
            <span class="n">uncor_energy</span> <span class="o">=</span> <span class="n">vac_def</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
            <span class="n">formation_energy</span> <span class="o">=</span> <span class="n">uncor_energy</span> <span class="o">+</span> <span class="n">mu_vals</span><span class="p">[</span><span class="n">ind</span><span class="p">]</span>
            <span class="n">en</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">formation_energy</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">en</span>

    <span class="n">en_res</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">new_mu_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
        <span class="n">mu_val</span> <span class="o">=</span> <span class="n">new_mu_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
        <span class="n">en_res</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">compute_vac_formation_energies</span><span class="p">(</span><span class="n">mu_val</span><span class="p">))</span>

    <span class="n">en_data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;x_label&#39;</span><span class="p">:</span> <span class="n">els</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39; mole fraction&#39;</span><span class="p">,</span> <span class="s1">&#39;x&#39;</span><span class="p">:</span> <span class="p">[]}</span>
    <span class="n">en_data</span><span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">dat</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">dat</span> <span class="ow">in</span> <span class="n">res1</span><span class="p">]</span>  <span class="c1"># x-axis data</span>

    <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">y_data</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">vac_def</span> <span class="ow">in</span> <span class="n">vac_defs</span><span class="p">:</span>
        <span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">data</span> <span class="ow">in</span> <span class="n">en_res</span><span class="p">]</span>
        <span class="n">site_specie</span> <span class="o">=</span> <span class="n">vac_def</span><span class="p">[</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span>
        <span class="n">ind</span> <span class="o">=</span> <span class="n">specie_order</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">site_specie</span><span class="p">)</span>
        <span class="n">specie_ind</span> <span class="o">=</span> <span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="n">specie_ind</span><span class="p">]</span>
        <span class="n">specie_ind_del</span> <span class="o">=</span> <span class="n">indices</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">cur_ind</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="n">vac_string</span> <span class="o">=</span> <span class="s2">&quot;$Vac_{&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">specie_ind_del</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">label</span> <span class="o">=</span> <span class="n">vac_string</span> <span class="o">+</span> <span class="n">site_specie</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">label</span> <span class="o">=</span> <span class="n">vac_string</span> <span class="o">+</span> <span class="n">site_specie</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">cur_ind</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
        <span class="n">y_data</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;data&#39;</span><span class="p">:</span> <span class="n">data</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">label</span><span class="p">})</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">compute_as_formation_energies</span><span class="p">(</span><span class="n">mu_vals</span><span class="p">):</span>
        <span class="n">en</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">as_def</span> <span class="ow">in</span> <span class="n">antisite_defs</span><span class="p">:</span>
            <span class="n">site_specie</span> <span class="o">=</span> <span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span>
            <span class="n">sub_specie</span> <span class="o">=</span> <span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;substitution_specie&#39;</span><span class="p">]</span>
            <span class="n">ind1</span> <span class="o">=</span> <span class="n">specie_order</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">site_specie</span><span class="p">)</span>
            <span class="n">ind2</span> <span class="o">=</span> <span class="n">specie_order</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">sub_specie</span><span class="p">)</span>
            <span class="n">uncor_energy</span> <span class="o">=</span> <span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
            <span class="n">form_en</span> <span class="o">=</span> <span class="n">uncor_energy</span> <span class="o">+</span> <span class="n">mu_vals</span><span class="p">[</span><span class="n">ind1</span><span class="p">]</span> <span class="o">-</span> <span class="n">mu_vals</span><span class="p">[</span><span class="n">ind2</span><span class="p">]</span>
            <span class="n">en</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">form_en</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">en</span>

    <span class="n">en_res</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">new_mu_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
        <span class="n">mu_val</span> <span class="o">=</span> <span class="n">new_mu_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
        <span class="n">en_res</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">compute_as_formation_energies</span><span class="p">(</span><span class="n">mu_val</span><span class="p">))</span>
    <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">for</span> <span class="n">as_def</span> <span class="ow">in</span> <span class="n">antisite_defs</span><span class="p">:</span>
        <span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">data</span> <span class="ow">in</span> <span class="n">en_res</span><span class="p">]</span>
        <span class="n">site_specie</span> <span class="o">=</span> <span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span>
        <span class="n">sub_specie</span> <span class="o">=</span> <span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;substitution_specie&#39;</span><span class="p">]</span>
        <span class="n">specie_ind</span> <span class="o">=</span> <span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="n">specie_ind</span><span class="p">]</span>
        <span class="n">specie_ind_del</span> <span class="o">=</span> <span class="n">indices</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">cur_ind</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">specie_ind_del</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;$&#39;</span> <span class="o">+</span> <span class="n">sub_specie</span> <span class="o">+</span> <span class="s1">&#39;_{&#39;</span> <span class="o">+</span> <span class="n">site_specie</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;$&#39;</span> <span class="o">+</span> <span class="n">sub_specie</span> <span class="o">+</span> <span class="s1">&#39;_{&#39;</span> <span class="o">+</span> <span class="n">site_specie</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">cur_ind</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
        <span class="n">y_data</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;data&#39;</span><span class="p">:</span> <span class="n">data</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">label</span><span class="p">})</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="n">en_data</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">y_data</span>

    <span class="c1"># Return chem potential as well</span>
    <span class="n">mu_data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;x_label&#39;</span><span class="p">:</span> <span class="n">els</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39; mole fraction&#39;</span><span class="p">,</span> <span class="s1">&#39;x&#39;</span><span class="p">:</span> <span class="p">[]}</span>
    <span class="n">mu_data</span><span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">dat</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">dat</span> <span class="ow">in</span> <span class="n">res1</span><span class="p">]</span>  <span class="c1"># x-axis data</span>

    <span class="n">y_data</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">):</span>
        <span class="n">specie</span> <span class="o">=</span> <span class="n">specie_order</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
        <span class="n">mus</span> <span class="o">=</span> <span class="p">[</span><span class="n">new_mu_dict</span><span class="p">[</span><span class="n">key</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">new_mu_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">())]</span>
        <span class="n">y_data</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;data&#39;</span><span class="p">:</span> <span class="n">mus</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">specie</span><span class="p">})</span>
    <span class="n">mu_data</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">y_data</span>

    <span class="k">return</span> <span class="n">conc_data</span><span class="p">,</span> <span class="n">en_data</span><span class="p">,</span> <span class="n">mu_data</span>


<div class="viewcode-block" id="compute_defect_density"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.dilute_solution_model.html#pymatgen.analysis.defects.dilute_solution_model.compute_defect_density">[docs]</a><span class="k">def</span> <span class="nf">compute_defect_density</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">e0</span><span class="p">,</span> <span class="n">vac_defs</span><span class="p">,</span> <span class="n">antisite_defs</span><span class="p">,</span> <span class="n">T</span><span class="o">=</span><span class="mi">800</span><span class="p">,</span> <span class="n">trial_chem_pot</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">plot_style</span><span class="o">=</span><span class="s2">&quot;highcharts&quot;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Wrapper for the dilute_solution_model.</span>
<span class="sd">    The computed plot data is prepared based on plot_style.</span>

<span class="sd">    Args:</span>
<span class="sd">        structure: pymatgen.core.structure.Structure object representing the</span>
<span class="sd">            primitive or unitcell of the crystal.</span>
<span class="sd">        e0: The total energy of the undefected system.</span>
<span class="sd">            This is E0 from VASP calculation.</span>
<span class="sd">        vac_defs: List of vacancy defect parameters in the dictionary format.</span>
<span class="sd">            The keys of the dict associated with each vacancy defect are</span>
<span class="sd">            1) site_index, 2) site_specie, 3) site_multiplicity, and</span>
<span class="sd">            4) energy. 1-3 can be obtained from</span>
<span class="sd">            pymatgen.analysis.defects.point_defects.Vacancy class.</span>
<span class="sd">            Site index is expected to start with 1 (fortran index).</span>
<span class="sd">        antisite_defs: List of antisite defect parameters in the dictionary</span>
<span class="sd">            format. The keys of the dict associated with each antisite defect</span>
<span class="sd">            are 1) site_index, 2) site_specie, 3) site_multiplicity,</span>
<span class="sd">            4) substitution_specie, and 5) energy. 1-3 can be obtained</span>
<span class="sd">            from pymatgen.analysis.defects.point_defects.Vacancy class.</span>
<span class="sd">        T: Temperature in Kelvin</span>
<span class="sd">        trial_chem_pot (optional): Trial chemical potentials to speedup</span>
<span class="sd">            the plot generation. Format is {el1:mu1,...}</span>
<span class="sd">        plot_style (string): Allowed options are</span>
<span class="sd">            1) highcharts (default)</span>
<span class="sd">            2) gnuplot</span>

<span class="sd">    Returns:</span>
<span class="sd">        The plot data is generated and returned in asked format.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">conc_data</span><span class="p">,</span> <span class="n">en_data</span><span class="p">,</span> <span class="n">mu_data</span> <span class="o">=</span> <span class="n">dilute_solution_model</span><span class="p">(</span>
        <span class="n">structure</span><span class="p">,</span> <span class="n">e0</span><span class="p">,</span> <span class="n">vac_defs</span><span class="p">,</span> <span class="n">antisite_defs</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">trial_chem_pot</span><span class="o">=</span><span class="n">trial_chem_pot</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">plot_style</span> <span class="o">==</span> <span class="s1">&#39;highcharts&#39;</span><span class="p">:</span>
        <span class="s2">&quot;Energy data is ignored in this mode&quot;</span>
        <span class="n">hgh_chrt_data</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">hgh_chrt_data</span><span class="p">[</span><span class="s1">&#39;xAxis&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">conc_data</span><span class="p">[</span><span class="s1">&#39;x_label&#39;</span><span class="p">]</span>
        <span class="n">hgh_chrt_data</span><span class="p">[</span><span class="s1">&#39;yAxis&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">conc_data</span><span class="p">[</span><span class="s1">&#39;y_label&#39;</span><span class="p">]</span>

        <span class="n">series</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">conc_data</span><span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">y_data</span> <span class="ow">in</span> <span class="n">conc_data</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]:</span>
            <span class="n">y</span> <span class="o">=</span> <span class="n">y_data</span><span class="p">[</span><span class="s1">&#39;data&#39;</span><span class="p">]</span>
            <span class="n">xy</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
            <span class="n">xy</span> <span class="o">=</span> <span class="p">[</span><span class="nb">list</span><span class="p">(</span><span class="n">el</span><span class="p">)</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">xy</span><span class="p">]</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">y_data</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">&#39;$&#39;</span><span class="p">)</span>
            <span class="n">flds</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">)</span>
            <span class="n">def_string</span> <span class="o">=</span> <span class="n">flds</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">site_string</span> <span class="o">=</span> <span class="n">flds</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">def_string</span> <span class="o">+</span> <span class="s2">&quot;&lt;sub&gt;&quot;</span> <span class="o">+</span> <span class="n">site_string</span> <span class="o">+</span> <span class="s2">&quot;&lt;/sub&gt;&quot;</span>
            <span class="c1"># series.append({&#39;data&#39;:xy, &#39;name&#39;:y_data[&#39;name&#39;]})</span>
            <span class="n">series</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;data&#39;</span><span class="p">:</span> <span class="n">xy</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">name</span><span class="p">})</span>
        <span class="n">hgh_chrt_data</span><span class="p">[</span><span class="s1">&#39;series&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">series</span>
        <span class="k">return</span> <span class="n">hgh_chrt_data</span>
    <span class="k">elif</span> <span class="n">plot_style</span> <span class="o">==</span> <span class="s1">&#39;gnuplot&#39;</span><span class="p">:</span>

        <span class="k">def</span> <span class="nf">data_to_rows</span><span class="p">(</span><span class="n">inp_data</span><span class="p">):</span>
            <span class="n">rows</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">labels</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">inp_data</span><span class="p">[</span><span class="s1">&#39;x_label&#39;</span><span class="p">])</span>
            <span class="n">labels</span> <span class="o">+=</span> <span class="p">[</span><span class="n">y</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">inp_data</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]]</span>
            <span class="c1"># labels.sort()</span>
            <span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;#&#39;</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">labels</span><span class="p">))</span>
            <span class="n">m</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">inp_data</span><span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">):</span>
                <span class="n">data</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">inp_data</span><span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
                <span class="n">data</span> <span class="o">+=</span> <span class="p">[</span><span class="n">y</span><span class="p">[</span><span class="s1">&#39;data&#39;</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">inp_data</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]]</span>
                <span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">data</span><span class="p">]</span>
                <span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">data</span><span class="p">))))</span>
            <span class="k">return</span> <span class="n">rows</span>

        <span class="n">conc_rows</span> <span class="o">=</span> <span class="n">data_to_rows</span><span class="p">(</span><span class="n">conc_data</span><span class="p">)</span>
        <span class="n">en_rows</span> <span class="o">=</span> <span class="n">data_to_rows</span><span class="p">(</span><span class="n">en_data</span><span class="p">)</span>
        <span class="n">mu_rows</span> <span class="o">=</span> <span class="n">data_to_rows</span><span class="p">(</span><span class="n">mu_data</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">conc_rows</span><span class="p">,</span> <span class="n">en_rows</span><span class="p">,</span> <span class="n">mu_rows</span></div>


<span class="c1"># solute_site_preference_finder is based on dilute_solution_model and so most</span>
<span class="c1"># of the code is same. However differences exist in setting up and processing</span>
<span class="c1"># hence new function</span>
<div class="viewcode-block" id="solute_site_preference_finder"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.dilute_solution_model.html#pymatgen.analysis.defects.dilute_solution_model.solute_site_preference_finder">[docs]</a><span class="k">def</span> <span class="nf">solute_site_preference_finder</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span>
                                  <span class="n">e0</span><span class="p">,</span>
                                  <span class="n">T</span><span class="p">,</span>
                                  <span class="n">vac_defs</span><span class="p">,</span>
                                  <span class="n">antisite_defs</span><span class="p">,</span>
                                  <span class="n">solute_defs</span><span class="p">,</span>
                                  <span class="n">solute_concen</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span>
                                  <span class="n">trial_chem_pot</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the solute defect densities using dilute solution model.</span>
<span class="sd">    Args:</span>
<span class="sd">        structure: pymatgen.core.structure.Structure object representing the</span>
<span class="sd">            primitive or unitcell of the crystal.</span>
<span class="sd">        e0: The total energy of the undefected system.</span>
<span class="sd">            This is E0 from VASP calculation.</span>
<span class="sd">        T: Temperature in Kelvin</span>
<span class="sd">        vac_defs: List of vacancy defect parameters in the dictionary format.</span>
<span class="sd">            The keys of the dict associated with each vacancy defect are</span>
<span class="sd">            1) site_index, 2) site_specie, 3) site_multiplicity, and</span>
<span class="sd">            4) energy. 1-3 can be obtained from</span>
<span class="sd">            pymatgen.analysis.defects.point_defects.Vacancy class.</span>
<span class="sd">            Site index is expected to start with 1 (fortran index).</span>
<span class="sd">        antisite_defs: List of antisite defect parameters in the dictionary</span>
<span class="sd">            format. The keys of the dict associated with each antisite</span>
<span class="sd">            defect are 1) site_index, 2) site_specie, 3) site_multiplicity,</span>
<span class="sd">            4) substitution_specie, and 5) energy. 1-3 can be obtained</span>
<span class="sd">            from pymatgen.analysis.defects.point_defects.Vacancy class.</span>
<span class="sd">        solute_defs: List of solute defect parameters in the dictionary</span>
<span class="sd">            format. Similary to that of antisite defs, wtih solute specie</span>
<span class="sd">            specified in substitution_specie</span>
<span class="sd">        solute_concen: Solute concentration (in fractional value)</span>
<span class="sd">        trial_chem_pot: Trial chemical potentials to speedup the plot</span>
<span class="sd">            generation. Format is {el1:mu1,...}</span>

<span class="sd">    Returns:</span>
<span class="sd">        plot_data: The data for plotting the solute defect concentration.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">_check_input</span><span class="p">(</span><span class="n">vac_defs</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Vacancy energy is not defined&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">_check_input</span><span class="p">(</span><span class="n">antisite_defs</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Antisite energy is not defined&#39;</span><span class="p">)</span>

    <span class="n">formation_energies</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;vacancies&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">vac_defs</span><span class="p">)</span>
    <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;antisites&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">antisite_defs</span><span class="p">)</span>
    <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;solute&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">solute_defs</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">vac</span> <span class="ow">in</span> <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;vacancies&#39;</span><span class="p">]:</span>
        <span class="k">del</span> <span class="n">vac</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
    <span class="k">for</span> <span class="n">asite</span> <span class="ow">in</span> <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;antisites&#39;</span><span class="p">]:</span>
        <span class="k">del</span> <span class="n">asite</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
    <span class="k">for</span> <span class="n">solute</span> <span class="ow">in</span> <span class="n">formation_energies</span><span class="p">[</span><span class="s1">&#39;solute&#39;</span><span class="p">]:</span>
        <span class="k">del</span> <span class="n">solute</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
    <span class="c1"># Setup the system</span>
    <span class="n">site_species</span> <span class="o">=</span> <span class="p">[</span><span class="n">vac_def</span><span class="p">[</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">vac_def</span> <span class="ow">in</span> <span class="n">vac_defs</span><span class="p">]</span>
    <span class="n">solute_specie</span> <span class="o">=</span> <span class="n">solute_defs</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;substitution_specie&#39;</span><span class="p">]</span>
    <span class="n">site_species</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">solute_specie</span><span class="p">)</span>
    <span class="n">multiplicity</span> <span class="o">=</span> <span class="p">[</span><span class="n">vac_def</span><span class="p">[</span><span class="s1">&#39;site_multiplicity&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">vac_def</span> <span class="ow">in</span> <span class="n">vac_defs</span><span class="p">]</span>
    <span class="n">m</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">site_species</span><span class="p">))</span>  <span class="c1"># distinct species</span>
    <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">vac_defs</span><span class="p">)</span>  <span class="c1"># inequivalent sites</span>

    <span class="c1"># Reduce the system and associated parameters such that only distinctive</span>
    <span class="c1"># atoms are retained</span>
    <span class="n">comm_div</span> <span class="o">=</span> <span class="n">gcd</span><span class="p">(</span><span class="o">*</span><span class="nb">tuple</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">))</span>
    <span class="n">multiplicity</span> <span class="o">=</span> <span class="p">[</span><span class="n">val</span> <span class="o">/</span> <span class="n">comm_div</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">multiplicity</span><span class="p">]</span>
    <span class="n">multiplicity</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
    <span class="n">e0</span> <span class="o">=</span> <span class="n">e0</span> <span class="o">/</span> <span class="n">comm_div</span>
    <span class="n">T</span> <span class="o">=</span> <span class="n">Float</span><span class="p">(</span><span class="n">T</span><span class="p">)</span>

    <span class="c1"># c0 = np.diag(multiplicity)</span>
    <span class="n">c0</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">diag</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
    <span class="n">c0</span><span class="p">[</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">mu</span> <span class="o">=</span> <span class="p">[</span><span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;mu&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]</span>

    <span class="c1"># Generate maps for hashing</span>
    <span class="c1"># Generate specie-&gt;mu map and use it for site-&gt;mu map</span>
    <span class="n">specie_order</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># Contains hash for site-&gt;mu map    Eg: [Al, Ni]</span>
    <span class="n">site_specie_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>  <span class="c1"># Eg: {Ni, Al}</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">site_species</span><span class="p">)):</span>
        <span class="n">site_specie</span> <span class="o">=</span> <span class="n">site_species</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">site_specie</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">site_specie_set</span><span class="p">:</span>
            <span class="n">site_specie_set</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">site_specie</span><span class="p">)</span>
            <span class="n">specie_order</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">site_specie</span><span class="p">)</span>
    <span class="n">site_mu_map</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># Eg: [mu0,mu0,mu0,mu1] where mu0-&gt;Al, and mu1-&gt;Ni</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">site_species</span><span class="p">)):</span>
        <span class="n">site_specie</span> <span class="o">=</span> <span class="n">site_species</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">j</span> <span class="o">=</span> <span class="n">specie_order</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">site_specie</span><span class="p">)</span>
        <span class="n">site_mu_map</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">j</span><span class="p">)</span>
    <span class="n">specie_site_index_map</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># Eg: [(0,3),(3,4)] for Al &amp; Ni</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">):</span>
        <span class="n">low_ind</span> <span class="o">=</span> <span class="n">site_species</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">specie_order</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">m</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">hgh_ind</span> <span class="o">=</span> <span class="n">site_species</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">specie_order</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">hgh_ind</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">site_species</span><span class="p">)</span>
        <span class="n">specie_site_index_map</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">low_ind</span><span class="p">,</span> <span class="n">hgh_ind</span><span class="p">))</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    dC: delta concentration matrix:</span>
<span class="sd">    dC[i,j,k]: Concentration change of atom i, due to presence of atom</span>
<span class="sd">    j on lattice site k</span>
<span class="sd">    Special case is [i,i,i] which is considered as vacancy</span>
<span class="sd">    Few cases: dC[i,i,i] = -1 due to being vacancy special case</span>
<span class="sd">                dC[k,k,i] = +1 due to increment in k at i lattice if i</span>
<span class="sd">                               lattice type is of different element</span>
<span class="sd">                dC[i,k,i] = -1 due to decrement of ith type atom due to</span>
<span class="sd">                presence of kth type atom on ith sublattice and kth type</span>
<span class="sd">                atom specie is different from ith sublattice atom specie</span>
<span class="sd">                dC[i,k,k] = 0 due to no effect on ith type atom</span>
<span class="sd">                dC[i,j,k] = 0 if i!=j!=k</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">dC</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">j</span> <span class="ow">and</span> <span class="n">site_species</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">!=</span> <span class="n">site_species</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="ow">and</span> \
                        <span class="n">site_species</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">!=</span> <span class="n">site_species</span><span class="p">:</span>
                    <span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">k</span><span class="p">:</span>
                    <span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
    <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="n">dC</span><span class="p">[</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">i</span> <span class="o">!=</span> <span class="n">j</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">site_species</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="n">site_species</span><span class="p">[</span><span class="n">k</span><span class="p">]:</span>
                    <span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">for</span> <span class="n">ind_map</span> <span class="ow">in</span> <span class="n">specie_site_index_map</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">ind_map</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">ind_map</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">index1</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ind_map</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">ind_map</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
                <span class="k">for</span> <span class="n">index2</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ind_map</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                        <span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">index1</span><span class="p">,</span> <span class="n">index2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">for</span> <span class="n">index2</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ind_map</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">n</span><span class="p">):</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                        <span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">index1</span><span class="p">,</span> <span class="n">index2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="c1"># dE matrix: Flip energies (or raw defect energies)</span>
    <span class="n">els</span> <span class="o">=</span> <span class="p">[</span><span class="n">vac_def</span><span class="p">[</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">vac_def</span> <span class="ow">in</span> <span class="n">vac_defs</span><span class="p">]</span>
    <span class="n">dE</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
        <span class="n">dE</span><span class="o">.</span><span class="n">append</span><span class="p">([])</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">dE</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">j</span><span class="p">:</span>
                <span class="n">dE</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">vac_defs</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">sub_specie</span> <span class="o">=</span> <span class="n">vac_defs</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span>
                <span class="n">site_specie</span> <span class="o">=</span> <span class="n">vac_defs</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">site_specie</span> <span class="o">==</span> <span class="n">sub_specie</span><span class="p">:</span>
                    <span class="n">dE</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">as_def</span> <span class="ow">in</span> <span class="n">antisite_defs</span><span class="p">:</span>
                        <span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;site_index&#39;</span><span class="p">])</span> <span class="o">==</span> <span class="n">j</span> <span class="o">+</span> <span class="mi">1</span> <span class="ow">and</span> \
                                <span class="n">sub_specie</span> <span class="o">==</span> <span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;substitution_specie&#39;</span><span class="p">]:</span>
                            <span class="n">dE</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">as_def</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
                            <span class="k">break</span>
        <span class="c1"># Solute</span>
        <span class="n">site_specie</span> <span class="o">=</span> <span class="n">vac_defs</span><span class="p">[</span><span class="n">j</span><span class="p">][</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">solute_def</span> <span class="ow">in</span> <span class="n">solute_defs</span><span class="p">:</span>
            <span class="n">def_site_ind</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">solute_def</span><span class="p">[</span><span class="s1">&#39;site_index&#39;</span><span class="p">])</span>
            <span class="n">def_site_specie</span> <span class="o">=</span> <span class="n">solute_def</span><span class="p">[</span><span class="s1">&#39;site_specie&#39;</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">def_site_specie</span> <span class="o">==</span> <span class="n">site_specie</span> <span class="ow">and</span> <span class="n">def_site_ind</span> <span class="o">==</span> <span class="n">j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">dE</span><span class="p">[</span><span class="n">n</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">solute_def</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
                <span class="k">break</span>

    <span class="n">dE</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">dE</span><span class="p">)</span>
    <span class="c1"># np.where(dE == np.array(None), 0, dE)</span>

    <span class="c1"># Initialization for concentrations</span>
    <span class="c1"># c(i,p) == presence of ith type atom on pth type site</span>
    <span class="n">c</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">p</span><span class="p">]</span> <span class="o">=</span> <span class="n">Integer</span><span class="p">(</span><span class="n">c0</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">p</span><span class="p">])</span>
            <span class="n">site_flip_contribs</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">epi</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
                <span class="n">sum_mu</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">mu</span><span class="p">[</span><span class="n">site_mu_map</span><span class="p">[</span><span class="n">j</span><span class="p">]]</span> <span class="o">*</span> <span class="n">Integer</span><span class="p">(</span><span class="n">dC</span><span class="p">[</span><span class="n">j</span><span class="p">,</span> <span class="n">epi</span><span class="p">,</span> <span class="n">p</span><span class="p">])</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)])</span>
                <span class="n">flip</span> <span class="o">=</span> <span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">epi</span><span class="p">,</span> <span class="n">p</span><span class="p">]</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">dE</span><span class="p">[</span><span class="n">epi</span><span class="p">,</span> <span class="n">p</span><span class="p">]</span> <span class="o">-</span> <span class="n">sum_mu</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">flip</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">site_flip_contribs</span><span class="p">:</span>
                    <span class="n">site_flip_contribs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">flip</span><span class="p">)</span>
                    <span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">p</span><span class="p">]</span> <span class="o">+=</span> <span class="n">flip</span>
    <span class="n">host_c</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">host_c</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">p</span><span class="p">]</span> <span class="o">=</span> <span class="n">Integer</span><span class="p">(</span><span class="n">c0</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">p</span><span class="p">])</span>
            <span class="n">site_flip_contribs</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">epi</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                <span class="n">sum_mu</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">mu</span><span class="p">[</span><span class="n">site_mu_map</span><span class="p">[</span><span class="n">j</span><span class="p">]]</span> <span class="o">*</span> <span class="n">Integer</span><span class="p">(</span><span class="n">dC</span><span class="p">[</span><span class="n">j</span><span class="p">,</span> <span class="n">epi</span><span class="p">,</span> <span class="n">p</span><span class="p">])</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)])</span>
                <span class="n">flip</span> <span class="o">=</span> <span class="n">dC</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">epi</span><span class="p">,</span> <span class="n">p</span><span class="p">]</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">dE</span><span class="p">[</span><span class="n">epi</span><span class="p">,</span> <span class="n">p</span><span class="p">]</span> <span class="o">-</span> <span class="n">sum_mu</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">flip</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">site_flip_contribs</span><span class="p">:</span>
                    <span class="n">site_flip_contribs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">flip</span><span class="p">)</span>
                    <span class="n">host_c</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">p</span><span class="p">]</span> <span class="o">+=</span> <span class="n">flip</span>

    <span class="c1"># specie_concen = [sum(mult[ind[0]:ind[1]]) for ind in specie_site_index_map]</span>
    <span class="c1"># total_c = [sum(c[ind[0]:ind[1]]) for ind in specie_site_index_map]</span>
    <span class="n">total_c</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="n">specie_site_index_map</span><span class="p">:</span>
        <span class="n">val</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="o">*</span><span class="n">ind</span><span class="p">):</span>
            <span class="n">sum_i</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">c</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">*</span> <span class="n">multiplicity</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)])</span>
            <span class="n">val</span> <span class="o">+=</span> <span class="n">sum_i</span>
        <span class="n">total_c</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>

    <span class="n">c_ratio</span> <span class="o">=</span> <span class="p">[</span><span class="n">total_c</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">total_c</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]</span>

    <span class="n">host_total_c</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="n">specie_site_index_map</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
        <span class="n">val</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="o">*</span><span class="n">ind</span><span class="p">):</span>
            <span class="n">sum_i</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">host_c</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">*</span> <span class="n">multiplicity</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)])</span>
            <span class="n">val</span> <span class="o">+=</span> <span class="n">sum_i</span>
        <span class="n">host_total_c</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>

    <span class="n">host_c_ratio</span> <span class="o">=</span> <span class="p">[</span><span class="n">host_total_c</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">host_total_c</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span>

    <span class="c1"># Expression for Omega, the Grand Potential</span>
    <span class="n">omega1</span> <span class="o">=</span> <span class="n">e0</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">([</span><span class="n">mu</span><span class="p">[</span><span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">*</span> <span class="nb">sum</span><span class="p">(</span><span class="n">c0</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:])</span> <span class="o">*</span> <span class="n">multiplicity</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)])</span>
    <span class="n">omega</span> <span class="o">=</span> <span class="n">omega1</span>

    <span class="n">used_dEs</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">p_r</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">epi</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">sum_mu1</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">mu</span><span class="p">[</span><span class="n">site_mu_map</span><span class="p">[</span><span class="n">j</span><span class="p">]]</span> <span class="o">*</span> <span class="n">Integer</span><span class="p">(</span><span class="n">dC</span><span class="p">[</span><span class="n">j</span><span class="p">,</span> <span class="n">epi</span><span class="p">,</span> <span class="n">p_r</span><span class="p">])</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)])</span>
            <span class="n">sum_mu</span> <span class="o">=</span> <span class="n">sum_mu1</span> <span class="o">-</span> <span class="n">mu</span><span class="p">[</span><span class="n">site_mu_map</span><span class="p">[</span><span class="n">n</span><span class="p">]]</span> <span class="o">*</span> <span class="n">dC</span><span class="p">[</span><span class="n">n</span><span class="p">,</span> <span class="n">epi</span><span class="p">,</span> <span class="n">p_r</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">p_r</span> <span class="o">!=</span> <span class="n">epi</span> <span class="ow">and</span> <span class="n">site_mu_map</span><span class="p">[</span><span class="n">p_r</span><span class="p">]</span> <span class="o">==</span> <span class="n">site_mu_map</span><span class="p">[</span><span class="n">epi</span><span class="p">]:</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="n">dE</span><span class="p">[</span><span class="n">epi</span><span class="p">,</span> <span class="n">p_r</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">used_dEs</span><span class="p">:</span>
                <span class="n">omega1</span> <span class="o">-=</span> <span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span> <span class="o">*</span> <span class="n">multiplicity</span><span class="p">[</span><span class="n">p_r</span><span class="p">]</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">dE</span><span class="p">[</span><span class="n">epi</span><span class="p">,</span> <span class="n">p_r</span><span class="p">]</span> <span class="o">-</span> <span class="n">sum_mu1</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span><span class="p">))</span>
                <span class="n">omega</span> <span class="o">-=</span> <span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span> <span class="o">*</span> <span class="n">multiplicity</span><span class="p">[</span><span class="n">p_r</span><span class="p">]</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">dE</span><span class="p">[</span><span class="n">epi</span><span class="p">,</span> <span class="n">p_r</span><span class="p">]</span> <span class="o">-</span> <span class="n">sum_mu</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span><span class="p">))</span>
                <span class="n">used_dEs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dE</span><span class="p">[</span><span class="n">epi</span><span class="p">,</span> <span class="n">p_r</span><span class="p">])</span>

    <span class="c1"># Compute composition ranges</span>
    <span class="n">max_host_specie_concen</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">solute_concen</span>
    <span class="n">mult</span> <span class="o">=</span> <span class="n">multiplicity</span>
    <span class="n">specie_concen</span> <span class="o">=</span> <span class="p">[</span><span class="nb">sum</span><span class="p">(</span><span class="n">mult</span><span class="p">[</span><span class="n">ind</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">ind</span><span class="p">[</span><span class="mi">1</span><span class="p">]])</span> <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="n">specie_site_index_map</span><span class="p">]</span>
    <span class="n">host_specie_concen_ratio</span> <span class="o">=</span> <span class="p">[</span><span class="n">specie_concen</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">specie_concen</span><span class="p">)</span> <span class="o">*</span> <span class="n">max_host_specie_concen</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]</span>
    <span class="n">host_specie_concen_ratio</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">solute_concen</span>
    <span class="n">li</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">hi</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">comp1_min</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">[</span><span class="n">li</span><span class="p">:</span><span class="n">hi</span><span class="p">])</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">)</span> <span class="o">*</span> <span class="n">max_host_specie_concen</span> <span class="o">-</span> <span class="mf">0.01</span>
    <span class="n">comp1_max</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">[</span><span class="n">li</span><span class="p">:</span><span class="n">hi</span><span class="p">])</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">)</span> <span class="o">*</span> <span class="n">max_host_specie_concen</span> <span class="o">+</span> <span class="mf">0.01</span>
    <span class="n">delta</span> <span class="o">=</span> <span class="p">(</span><span class="n">comp1_max</span> <span class="o">-</span> <span class="n">comp1_min</span><span class="p">)</span> <span class="o">/</span> <span class="mf">50.0</span>

    <span class="c1"># def reduce_mu():</span>
    <span class="c1">#    omega = [e0 - sum([mu[site_mu_map[i]]*sum(c0[i,:]) for i in range(n)])]</span>
    <span class="c1">#    x = solve(omega)</span>
    <span class="c1">#    return x</span>
    <span class="k">def</span> <span class="nf">reduce_mu</span><span class="p">():</span>
        <span class="n">host_concen</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">solute_concen</span>
        <span class="n">new_c0</span> <span class="o">=</span> <span class="n">c0</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">float</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">new_c0</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">host_concen</span> <span class="o">*</span> <span class="n">c0</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">i</span><span class="p">]</span>
        <span class="n">new_c0</span><span class="p">[</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">solute_concen</span>
        <span class="n">omega</span> <span class="o">=</span> <span class="p">[</span><span class="n">e0</span> <span class="o">-</span> <span class="nb">sum</span><span class="p">([</span><span class="n">mu</span><span class="p">[</span><span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">*</span> <span class="nb">sum</span><span class="p">(</span><span class="n">new_c0</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:])</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)])]</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">solve</span><span class="p">(</span><span class="n">omega</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">x</span>

    <span class="k">def</span> <span class="nf">compute_solute_mu_by_lin_search</span><span class="p">(</span><span class="n">host_mu_vals</span><span class="p">):</span>
        <span class="c1"># Compute trial mu</span>
        <span class="n">mult</span> <span class="o">=</span> <span class="n">multiplicity</span>
        <span class="n">specie_concen</span> <span class="o">=</span> <span class="p">[</span><span class="nb">sum</span><span class="p">(</span><span class="n">mult</span><span class="p">[</span><span class="n">ind</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">ind</span><span class="p">[</span><span class="mi">1</span><span class="p">]])</span> <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="n">specie_site_index_map</span><span class="p">]</span>
        <span class="n">max_host_specie_concen</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">solute_concen</span>
        <span class="n">host_specie_concen_ratio</span> <span class="o">=</span> <span class="p">[</span><span class="n">specie_concen</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">specie_concen</span><span class="p">)</span> <span class="o">*</span> <span class="n">max_host_specie_concen</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]</span>
        <span class="n">host_specie_concen_ratio</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">solute_concen</span>
        <span class="n">y_vect</span> <span class="o">=</span> <span class="n">host_specie_concen_ratio</span>
        <span class="n">vector_func</span> <span class="o">=</span> <span class="p">[</span><span class="n">y_vect</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">c_ratio</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]</span>
        <span class="n">vector_func</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">omega</span><span class="p">)</span>
        <span class="n">mu_vals</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">m1_min</span> <span class="o">=</span> <span class="o">-</span><span class="mf">20.0</span>
        <span class="k">if</span> <span class="n">e0</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">m1_max</span> <span class="o">=</span> <span class="mi">10</span>  <span class="c1"># Search space needs to be modified</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">m1_max</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">m1</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">m1_min</span><span class="p">,</span> <span class="n">m1_max</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">):</span>
            <span class="n">trial_mus</span> <span class="o">=</span> <span class="n">host_mu_vals</span> <span class="o">+</span> <span class="p">[</span><span class="n">m1</span><span class="p">]</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">x</span> <span class="o">=</span> <span class="n">nsolve</span><span class="p">(</span><span class="n">vector_func</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">trial_mus</span><span class="p">,</span> <span class="n">module</span><span class="o">=</span><span class="s2">&quot;numpy&quot;</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">x</span><span class="p">:</span>
                    <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">mu_val</span><span class="p">)</span> <span class="k">for</span> <span class="n">mu_val</span> <span class="ow">in</span> <span class="n">x</span><span class="p">]</span>
                <span class="k">break</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="k">continue</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">mu_vals</span>

    <span class="k">def</span> <span class="nf">compute_mus</span><span class="p">():</span>

        <span class="c1"># Compute trial mu</span>
        <span class="n">mu_red</span> <span class="o">=</span> <span class="n">reduce_mu</span><span class="p">()</span>
        <span class="n">mult</span> <span class="o">=</span> <span class="n">multiplicity</span>
        <span class="n">specie_concen</span> <span class="o">=</span> <span class="p">[</span><span class="nb">sum</span><span class="p">(</span><span class="n">mult</span><span class="p">[</span><span class="n">ind</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">ind</span><span class="p">[</span><span class="mi">1</span><span class="p">]])</span> <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="n">specie_site_index_map</span><span class="p">]</span>
        <span class="n">max_host_specie_concen</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">solute_concen</span>
        <span class="n">host_specie_concen_ratio</span> <span class="o">=</span> <span class="p">[</span><span class="n">specie_concen</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">specie_concen</span><span class="p">)</span> <span class="o">*</span> <span class="n">max_host_specie_concen</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]</span>
        <span class="n">host_specie_concen_ratio</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">solute_concen</span>

        <span class="n">y_vect</span> <span class="o">=</span> <span class="n">host_specie_concen_ratio</span>
        <span class="n">vector_func</span> <span class="o">=</span> <span class="p">[</span><span class="n">y_vect</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">c_ratio</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">)]</span>
        <span class="n">vector_func</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">omega</span><span class="p">)</span>
        <span class="n">mu_vals</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">m_min</span> <span class="o">=</span> <span class="o">-</span><span class="mf">15.0</span>
        <span class="k">if</span> <span class="n">e0</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">m_max</span> <span class="o">=</span> <span class="mi">10</span>  <span class="c1"># Search space needs to be modified</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">m_max</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">m1</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">m_min</span><span class="p">,</span> <span class="n">m_max</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">m2</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">m_min</span><span class="p">,</span> <span class="n">m_max</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">):</span>
                <span class="n">m0</span> <span class="o">=</span> <span class="n">mu_red</span><span class="p">[</span><span class="n">mu</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span><span class="o">.</span><span class="n">subs</span><span class="p">([(</span><span class="n">mu</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">m1</span><span class="p">),</span> <span class="p">(</span><span class="n">mu</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">m2</span><span class="p">)])</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">mu_vals</span> <span class="o">=</span> <span class="n">nsolve</span><span class="p">(</span><span class="n">vector_func</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="p">[</span><span class="n">m0</span><span class="p">,</span> <span class="n">m1</span><span class="p">,</span> <span class="n">m2</span><span class="p">],</span> <span class="n">module</span><span class="o">=</span><span class="s2">&quot;numpy&quot;</span><span class="p">)</span>
                    <span class="c1"># Line needs to be modified to include all mus when n &gt; 2</span>
                <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">break</span>
            <span class="k">if</span> <span class="n">mu_vals</span><span class="p">:</span>
                <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">mu_val</span><span class="p">)</span> <span class="k">for</span> <span class="n">mu_val</span> <span class="ow">in</span> <span class="n">mu_vals</span><span class="p">]</span>
                <span class="k">break</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Couldn&#39;t find mus&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">mu_vals</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">trial_chem_pot</span><span class="p">:</span>
        <span class="c1"># Try computing mus by assuming one of the defects is dominant at 0.01</span>
        <span class="c1"># concen.  First vacancy is tried and then antisite</span>

        <span class="c1"># Generate trial mus assuming vacancy as dominant defect</span>
        <span class="c1"># for specie-0 at lower yval</span>
        <span class="n">li</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">hi</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">li1</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">hi1</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">spec_mult</span> <span class="o">=</span> <span class="p">[</span><span class="nb">sum</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">[</span><span class="n">li</span><span class="p">:</span><span class="n">hi</span><span class="p">]),</span> <span class="nb">sum</span><span class="p">(</span><span class="n">multiplicity</span><span class="p">[</span><span class="n">li1</span><span class="p">:</span><span class="n">hi1</span><span class="p">])]</span>
        <span class="n">ln_def_conc</span> <span class="o">=</span> <span class="mf">4.60517</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">li</span><span class="p">,</span> <span class="n">hi</span><span class="p">):</span>
            <span class="n">vac_flip_en</span> <span class="o">=</span> <span class="n">vac_defs</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="n">ln_def_conc</span> <span class="o">*</span> <span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span> <span class="o">-</span> <span class="n">vac_flip_en</span><span class="p">]</span>
            <span class="n">mu_vals</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">e0</span> <span class="o">-</span> <span class="n">spec_mult</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">mu_vals</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">/</span> <span class="n">spec_mult</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="n">comp_ratio</span> <span class="o">=</span> <span class="n">comp1_min</span>

            <span class="c1"># Test if the trial mus are good</span>
            <span class="n">vector_func</span> <span class="o">=</span> <span class="p">[</span><span class="n">comp_ratio</span> <span class="o">-</span> <span class="n">host_c_ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
            <span class="n">vector_func</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">omega1</span><span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">host_mu_vals</span> <span class="o">=</span> <span class="n">nsolve</span><span class="p">(</span><span class="n">vector_func</span><span class="p">,</span> <span class="n">mu</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">mu_vals</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">host_mu_vals</span><span class="p">:</span>
                    <span class="n">host_mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">mu_val</span><span class="p">)</span> <span class="k">for</span> <span class="n">mu_val</span> <span class="ow">in</span> <span class="n">host_mu_vals</span><span class="p">]</span>
                <span class="n">compute_solute_mu_by_lin_search</span><span class="p">(</span><span class="n">host_mu_vals</span><span class="p">)</span>
                <span class="k">break</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>  <span class="c1"># Go for antisite as dominant defect</span>
                <span class="n">mu_gs</span> <span class="o">=</span> <span class="p">[</span><span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;mu_gs&#39;</span> <span class="o">+</span> <span class="n">j</span><span class="o">.</span><span class="fm">__str__</span><span class="p">())</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span>

                <span class="n">eqs</span> <span class="o">=</span> <span class="p">[</span><span class="n">mu_gs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">mu_gs</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="p">(</span><span class="n">ln_def_conc</span> <span class="o">*</span> <span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span> <span class="o">-</span> <span class="n">antisite_defs</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="s1">&#39;energy&#39;</span><span class="p">])]</span>
                <span class="n">eqs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">spec_mult</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">mu_gs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">spec_mult</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">mu_gs</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">e0</span><span class="p">)</span>
                <span class="n">x</span> <span class="o">=</span> <span class="n">solve</span><span class="p">(</span><span class="n">eqs</span><span class="p">,</span> <span class="n">mu_gs</span><span class="p">)</span>
                <span class="n">host_mu_vals</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">keys</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">inp</span><span class="p">:</span> <span class="n">inp</span><span class="o">.</span><span class="n">name</span><span class="p">):</span>
                    <span class="n">host_mu_vals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">key</span><span class="p">])</span>
                <span class="n">vector_func</span> <span class="o">=</span> <span class="p">[</span><span class="n">comp_ratio</span> <span class="o">-</span> <span class="n">host_c_ratio</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
                <span class="n">vector_func</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">omega1</span><span class="p">)</span>

                <span class="k">try</span><span class="p">:</span>
                    <span class="n">host_mu_vals</span> <span class="o">=</span> <span class="n">nsolve</span><span class="p">(</span><span class="n">vector_func</span><span class="p">,</span> <span class="n">mu</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">host_mu_vals</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">host_mu_vals</span><span class="p">:</span>
                        <span class="n">host_mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">mu_val</span><span class="p">)</span> <span class="k">for</span> <span class="n">mu_val</span> <span class="ow">in</span> <span class="n">host_mu_vals</span><span class="p">]</span>
                    <span class="n">mu_vals</span> <span class="o">=</span> <span class="n">compute_solute_mu_by_lin_search</span><span class="p">(</span><span class="n">host_mu_vals</span><span class="p">)</span>
                    <span class="k">break</span>
                <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>  <span class="c1"># Go to the default option (search the space)</span>
                    <span class="k">pass</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="n">compute_mus</span><span class="p">()</span>

    <span class="k">else</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="n">trial_chem_pot</span><span class="p">[</span><span class="n">element</span><span class="p">]</span> <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">specie_order</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="n">mu_vals</span> <span class="o">=</span> <span class="n">compute_mus</span><span class="p">()</span>

    <span class="c1"># Compile mu&#39;s for all composition ratios in the range</span>
    <span class="c1"># +/- 1% from the stoichiometry</span>
    <span class="n">result</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">comp1_min</span><span class="p">,</span> <span class="n">comp1_max</span> <span class="o">+</span> <span class="n">delta</span><span class="p">,</span> <span class="n">delta</span><span class="p">):</span>
        <span class="n">y_vect</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">y_vect</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
        <span class="n">y2</span> <span class="o">=</span> <span class="n">max_host_specie_concen</span> <span class="o">-</span> <span class="n">y</span>
        <span class="n">y_vect</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">y2</span><span class="p">)</span>
        <span class="n">y_vect</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">solute_concen</span><span class="p">)</span>
        <span class="n">vector_func</span> <span class="o">=</span> <span class="p">[</span><span class="n">y_vect</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">c_ratio</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">m</span><span class="p">)]</span>
        <span class="n">vector_func</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">omega</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">x</span> <span class="o">=</span> <span class="n">nsolve</span><span class="p">(</span><span class="n">vector_func</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">mu_vals</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">x</span><span class="p">:</span>
                <span class="n">mu_vals</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">mu_val</span><span class="p">)</span> <span class="k">for</span> <span class="n">mu_val</span> <span class="ow">in</span> <span class="n">x</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="n">result</span><span class="p">[</span><span class="n">y</span><span class="p">]</span> <span class="o">=</span> <span class="n">mu_vals</span>

    <span class="n">res</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="c1"># Compute the concentrations for all the compositions</span>
    <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
        <span class="n">mu_val</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
        <span class="n">total_c_val</span> <span class="o">=</span> <span class="p">[</span><span class="n">total_c</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mu_val</span><span class="p">)))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">total_c</span><span class="p">))]</span>
        <span class="n">c_val</span> <span class="o">=</span> <span class="n">c</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">mu_val</span><span class="p">)))</span>
        <span class="c1"># Concentration of first element/over total concen</span>
        <span class="n">res1</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">res1</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">total_c_val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">total_c_val</span><span class="p">)))</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">j</span><span class="p">:</span>  <span class="c1"># Vacancy</span>
                    <span class="n">vac_conc</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">mu_val</span><span class="p">[</span><span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">+</span> <span class="n">dE</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">i</span><span class="p">])</span> <span class="o">/</span> <span class="p">(</span><span class="n">k_B</span> <span class="o">*</span> <span class="n">T</span><span class="p">)))</span>
                    <span class="n">res1</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">vac_conc</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>  <span class="c1"># Antisite</span>
                    <span class="n">res1</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">c_val</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">/</span> <span class="n">c0</span><span class="p">[</span><span class="n">j</span><span class="p">,</span> <span class="n">j</span><span class="p">]))</span>
        <span class="n">res</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">res1</span><span class="p">)</span>

    <span class="n">res</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
    <span class="n">dtype</span> <span class="o">=</span> <span class="p">[(</span><span class="nb">str</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)]</span> <span class="o">+</span> <span class="p">[(</span><span class="nb">str</span><span class="p">(</span><span class="s1">&#39;y</span><span class="si">%d%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">)),</span> <span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)]</span>
    <span class="n">res1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">dtype</span><span class="p">),</span> <span class="n">order</span><span class="o">=</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

    <span class="n">conc</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
        <span class="n">conc</span><span class="o">.</span><span class="n">append</span><span class="p">([])</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">conc</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">([])</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>  <span class="c1"># Append vacancies</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="n">y1</span> <span class="o">=</span> <span class="p">[</span><span class="n">dat</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="n">i</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="n">j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">dat</span> <span class="ow">in</span> <span class="n">res1</span><span class="p">]</span>
            <span class="n">conc</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">y1</span>

    <span class="c1"># Compute solute site preference</span>
    <span class="c1"># Removing the functionality</span>
    <span class="c1"># site_pref_data = {}</span>
    <span class="sd">&quot;&quot;&quot;Because all the plots have identical x-points storing it in a</span>
<span class="sd">    single array&quot;&quot;&quot;</span>
    <span class="c1"># site_pref_data[&#39;x&#39;] = [dat[0][0] for dat in res1]         # x-axis data</span>
    <span class="c1"># Element whose composition is varied. For x-label</span>
    <span class="c1"># site_pref_data[&#39;x_label&#39;] = els[0]+ &quot;_mole_fraction&quot;</span>
    <span class="c1"># site_pref_data[&#39;y_label&#39;] = &quot;$&quot;+solute_specie+&quot;_{&quot;+els[0]+&quot;}/(&quot;+\</span>
    <span class="c1">#    solute_specie+&quot;_{&quot;+els[0]+&quot;}+&quot;+solute_specie+&quot;_{&quot;+els[1]+&quot;})$&quot;</span>

    <span class="c1"># y_data = []</span>
    <span class="c1"># inds = specie_site_index_map[m-1]</span>
    <span class="c1"># data1 = np.sum([multiplicity[0]*conc[ind][0] for ind in range(*inds)],axis=0)</span>
    <span class="c1"># data2 = np.sum([multiplicity[1]*conc[ind][1] for ind in range(*inds)],axis=0)</span>
    <span class="c1"># frac_data = data1/(data1+data2)</span>
    <span class="c1"># frac_data = frac_data.tolist()</span>
    <span class="c1"># y_data.append({&#39;data&#39;:frac_data})</span>

    <span class="c1"># site_pref_data[&#39;y&#39;] = y_data</span>

    <span class="c1">#  Return all defect concentrations</span>
    <span class="n">conc_data</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="sd">&quot;&quot;&quot;Because all the plots have identical x-points storing it in a</span>
<span class="sd">    single array&quot;&quot;&quot;</span>
    <span class="n">conc_data</span><span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">dat</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">dat</span> <span class="ow">in</span> <span class="n">res1</span><span class="p">]</span>  <span class="c1"># x-axis data</span>
    <span class="c1"># Element whose composition is varied. For x-label</span>
    <span class="n">conc_data</span><span class="p">[</span><span class="s1">&#39;x_label&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">els</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s2">&quot; mole fraction&quot;</span>
    <span class="n">conc_data</span><span class="p">[</span><span class="s1">&#39;y_label&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;Point defect concentration&quot;</span>

    <span class="n">y_data</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="c1"># Vacancy</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">conc</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">i</span><span class="p">]</span>
        <span class="n">specie</span> <span class="o">=</span> <span class="n">els</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">specie_ind</span> <span class="o">=</span> <span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="n">specie_ind</span><span class="p">]</span>
        <span class="n">specie_ind_del</span> <span class="o">=</span> <span class="n">indices</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">cur_ind</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="n">vac_string</span> <span class="o">=</span> <span class="s2">&quot;$Vac_{&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">specie_ind_del</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">label</span> <span class="o">=</span> <span class="n">vac_string</span> <span class="o">+</span> <span class="n">specie</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">label</span> <span class="o">=</span> <span class="n">vac_string</span> <span class="o">+</span> <span class="n">specie</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">cur_ind</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
        <span class="c1"># Plot data and legend info</span>
        <span class="n">y_data</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;data&#39;</span><span class="p">:</span> <span class="n">data</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">label</span><span class="p">})</span>

    <span class="c1"># Antisites and solute</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
        <span class="n">site_specie</span> <span class="o">=</span> <span class="n">els</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">specie_ind</span> <span class="o">=</span> <span class="n">site_mu_map</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="n">specie_ind</span><span class="p">]</span>
        <span class="n">specie_ind_del</span> <span class="o">=</span> <span class="n">indices</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">cur_ind</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">):</span>
            <span class="n">sub_specie</span> <span class="o">=</span> <span class="n">specie_order</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">sub_specie</span> <span class="o">==</span> <span class="n">site_specie</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">specie_ind_del</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;$&#39;</span> <span class="o">+</span> <span class="n">sub_specie</span> <span class="o">+</span> <span class="s1">&#39;_{&#39;</span> <span class="o">+</span> <span class="n">site_specie</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">label</span> <span class="o">=</span> <span class="s1">&#39;$&#39;</span> <span class="o">+</span> <span class="n">sub_specie</span> <span class="o">+</span> <span class="s1">&#39;_{&#39;</span> <span class="o">+</span> <span class="n">site_specie</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">cur_ind</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;}$&#39;</span>
            <span class="n">inds</span> <span class="o">=</span> <span class="n">specie_site_index_map</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
            <span class="c1"># TODO: Investigate the value below</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">([</span><span class="n">conc</span><span class="p">[</span><span class="n">ind</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="o">*</span><span class="n">inds</span><span class="p">)],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
            <span class="n">y_data</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;data&#39;</span><span class="p">:</span> <span class="n">data</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">label</span><span class="p">})</span>

    <span class="n">conc_data</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">y_data</span>
    <span class="c1"># return site_pref_data, conc_data</span>
    <span class="k">return</span> <span class="n">conc_data</span></div>


<div class="viewcode-block" id="solute_defect_density"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.dilute_solution_model.html#pymatgen.analysis.defects.dilute_solution_model.solute_defect_density">[docs]</a><span class="k">def</span> <span class="nf">solute_defect_density</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span>
                          <span class="n">e0</span><span class="p">,</span>
                          <span class="n">vac_defs</span><span class="p">,</span>
                          <span class="n">antisite_defs</span><span class="p">,</span>
                          <span class="n">solute_defs</span><span class="p">,</span>
                          <span class="n">solute_concen</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span>
                          <span class="n">T</span><span class="o">=</span><span class="mi">800</span><span class="p">,</span>
                          <span class="n">trial_chem_pot</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                          <span class="n">plot_style</span><span class="o">=</span><span class="s2">&quot;highchargs&quot;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Wrapper for the solute_site_preference_finder.</span>
<span class="sd">    The computed plot data is prepared based on plot_style.</span>

<span class="sd">    Args:</span>
<span class="sd">        structure: pymatgen.core.structure.Structure object representing the</span>
<span class="sd">            primitive or unitcell of the crystal.</span>
<span class="sd">        e0: The total energy of the undefected system.</span>
<span class="sd">            This is E0 from VASP calculation.</span>
<span class="sd">        vac_defs: List of vacancy defect parameters in the dictionary format.</span>
<span class="sd">            The keys of the dict associated with each vacancy defect are</span>
<span class="sd">            1) site_index, 2) site_specie, 3) site_multiplicity, and</span>
<span class="sd">            4) energy. 1-3 can be obtained from</span>
<span class="sd">            pymatgen.analysis.defects.point_defects.Vacancy class.</span>
<span class="sd">            Site index is expected to start with 1 (fortran index).</span>
<span class="sd">        antisite_defs: List of antisite defect parameters in the dictionary</span>
<span class="sd">            format. The keys of the dict associated with each antisite defect</span>
<span class="sd">            are 1) site_index, 2) site_specie, 3) site_multiplicity,</span>
<span class="sd">            4) substitution_specie, and 5) energy. 1-3 can be obtained</span>
<span class="sd">            from pymatgen.analysis.defects.point_defects.Vacancy class.</span>
<span class="sd">        solute_defs: List of solute defect parameters in the dictionary</span>
<span class="sd">            format. Similary to that of antisite defs, wtih solute specie</span>
<span class="sd">            specified in substitution_specie</span>
<span class="sd">        solute_concen: Solute concentration (in fractional value)</span>
<span class="sd">        T: Temperature in Kelvin</span>
<span class="sd">        trial_chem_pot (optional): Trial chemical potentials to speedup</span>
<span class="sd">            the plot generation. Format is {el1:mu1,...}</span>
<span class="sd">        plot_style (string): Allowed options are</span>
<span class="sd">            1) highcharts (default)</span>
<span class="sd">            2) gnuplot</span>

<span class="sd">    Returns:</span>
<span class="sd">        The plot data is generated and returned in asked format.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># solute_site_pref_data, def_conc_data = solute_site_preference_finder(</span>
    <span class="n">def_conc_data</span> <span class="o">=</span> <span class="n">solute_site_preference_finder</span><span class="p">(</span>
        <span class="n">structure</span><span class="p">,</span>
        <span class="n">e0</span><span class="p">,</span>
        <span class="n">T</span><span class="p">,</span>
        <span class="n">vac_defs</span><span class="p">,</span>
        <span class="n">antisite_defs</span><span class="p">,</span>
        <span class="n">solute_defs</span><span class="p">,</span>
        <span class="n">solute_concen</span><span class="o">=</span><span class="n">solute_concen</span><span class="p">,</span>
        <span class="n">trial_chem_pot</span><span class="o">=</span><span class="n">trial_chem_pot</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">plot_style</span> <span class="o">==</span> <span class="s1">&#39;highcharts&#39;</span><span class="p">:</span>
        <span class="s2">&quot;Energy data is ignored in this mode&quot;</span>
        <span class="n">hgh_chrt_data</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">hgh_chrt_data</span><span class="p">[</span><span class="s1">&#39;xAxis&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">def_conc_data</span><span class="p">[</span><span class="s1">&#39;x_label&#39;</span><span class="p">]</span>
        <span class="n">hgh_chrt_data</span><span class="p">[</span><span class="s1">&#39;yAxis&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">def_conc_data</span><span class="p">[</span><span class="s1">&#39;y_label&#39;</span><span class="p">]</span>

        <span class="n">series</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">def_conc_data</span><span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">y_data</span> <span class="ow">in</span> <span class="n">def_conc_data</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]:</span>
            <span class="n">y</span> <span class="o">=</span> <span class="n">y_data</span><span class="p">[</span><span class="s1">&#39;data&#39;</span><span class="p">]</span>
            <span class="n">xy</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
            <span class="n">xy</span> <span class="o">=</span> <span class="p">[</span><span class="nb">list</span><span class="p">(</span><span class="n">el</span><span class="p">)</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">xy</span><span class="p">]</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">y_data</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">&#39;$&#39;</span><span class="p">)</span>
            <span class="n">flds</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">)</span>
            <span class="n">def_string</span> <span class="o">=</span> <span class="n">flds</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">site_string</span> <span class="o">=</span> <span class="n">flds</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">def_string</span> <span class="o">+</span> <span class="s2">&quot;&lt;sub&gt;&quot;</span> <span class="o">+</span> <span class="n">site_string</span> <span class="o">+</span> <span class="s2">&quot;&lt;/sub&gt;&quot;</span>
            <span class="c1"># series.append({&#39;data&#39;:xy, &#39;name&#39;:y_data[&#39;name&#39;]})</span>
            <span class="n">series</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;data&#39;</span><span class="p">:</span> <span class="n">xy</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">name</span><span class="p">})</span>
        <span class="n">hgh_chrt_data</span><span class="p">[</span><span class="s1">&#39;series&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">series</span>
        <span class="k">return</span> <span class="n">hgh_chrt_data</span>
    <span class="k">elif</span> <span class="n">plot_style</span> <span class="o">==</span> <span class="s1">&#39;gnuplot&#39;</span><span class="p">:</span>

        <span class="k">def</span> <span class="nf">data_to_rows</span><span class="p">(</span><span class="n">inp_data</span><span class="p">,</span> <span class="n">y_lbl_flg</span><span class="p">):</span>
            <span class="n">rows</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">labels</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">inp_data</span><span class="p">[</span><span class="s1">&#39;x_label&#39;</span><span class="p">])</span>
            <span class="k">if</span> <span class="n">y_lbl_flg</span><span class="p">:</span>
                <span class="n">labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">inp_data</span><span class="p">[</span><span class="s1">&#39;y_label&#39;</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">labels</span> <span class="o">+=</span> <span class="p">[</span><span class="n">y</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">inp_data</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]]</span>
            <span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;#&#39;</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">labels</span><span class="p">))</span>
            <span class="n">m</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">inp_data</span><span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">m</span><span class="p">):</span>
                <span class="n">data</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">inp_data</span><span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
                <span class="n">data</span> <span class="o">+=</span> <span class="p">[</span><span class="n">y</span><span class="p">[</span><span class="s1">&#39;data&#39;</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">inp_data</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]]</span>
                <span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">data</span><span class="p">]</span>
                <span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">data</span><span class="p">))))</span>
            <span class="k">return</span> <span class="n">rows</span>

        <span class="c1"># solute_site_pref_rows = data_to_rows(solute_site_pref_data, True)</span>
        <span class="n">pt_def_conc_rows</span> <span class="o">=</span> <span class="n">data_to_rows</span><span class="p">(</span><span class="n">def_conc_data</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="c1"># return solute_site_pref_rows, pt_def_conc_rows</span>
        <span class="k">return</span> <span class="n">pt_def_conc_rows</span></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.defects.dilute_solution_model</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>